Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 28, 2022 10:12 pm GMT

100 Languages Speedrun: Episode 71: Factor

Forth language is dead, and its ideas never saw much adoption into the mainstream languages, but Forth-style languages pop up every now and then, without ever getting much traction.

One of such languages has been Factor. Let's see how it improves upon Forth.

Hello, World!

You can brew install factor, but it won't be in your path, instead it installs to a weird place.

There's also unrelated factor on Linux machines that factorizes numbers:

$ factor 420420: 2 2 3 5 7

Anyway, Factor. The first annoying issue is that a fresh Factor program comes with no library of any kind. No print, no +, literally nothing.

If we try this:

#!/Applications/factor/factor"Hello, World!" print

We get this error:

$ ./hello.factor./hello.factor3: "Hello, World!" print                        ^No word named print found in current vocabulary search path(U) Quotation: [ c-to-factor => ]    Word: c-to-factor(U) Quotation: [ [ (get-catchstack) push ] dip call => (get-catchstack) pop* ](O) Word: command-line-startup(O) Word: run-script(O) Word: run-file(O) Word: parse-file(O) Word: parse-stream(O) Word: parse-fresh(O) Word: (parse-lines)(O) Word: (parse-until)(O) Word: parse-until-step(O) Word: no-word(O) Word: throw-restarts(O) Method: M\ object throw(U) Quotation: [        OBJ-CURRENT-THREAD special-object error-thread set-global        current-continuation => error-continuation set-global        [ original-error set-global ] [ rethrow ] bi    ]

So it took me about 1 minute to seriously start hating the language. The reasonable error message would be something like:

No word named print found in current vocabulary search path. It is defined in the following namespaces: io.

But no, Factor is winning the prize for having the absolute worst error messages for the simplest scripts. And it's not like it's just a few libraries to remember, nope, Factor split core functionality among hundreds of micro-libraries, so enjoy your suffering if you try to code it.

Factor VSCode plugin is also of zero help here. Factor comes with an interactive app, which has help system, which you can sort of use to search this, but it's not really much improvement over just Googling it. It's a huge pain point until you memorize all the common imports.

Hello, World! Again

Once we figure out that print is in io, we can do this:

#!/Applications/factor/factorUSING: io ;"Hello, World!" print

Math

OK, let's add some numbers. For this we'll need 3 different imports!

#!/Applications/factor/factorUSING: io math math.parser ;400 20 + number>string print60 9 + number>string print
$ ./math.factor42069

Loop

Here's a simple loop that prints numbers 1 to 10, of course it needs another import:

#!/Applications/factor/factorUSING: io math math.parser kernel ;1[                ! 1  dup            ! 1 1  number>string  ! 1 "1"  print          ! 1  1              ! 1 1  +              ! 2  dup            ! 2 2  10             ! 2 2 10  <=             ! 2 t]loopdrop
$ ./loop.factor12345678910

We need to drop at the end, as if you have anything on the stack once Factor finishes, it will crash.

The ! is comment character. I added some examples what's goin to be the stack status after each command. In real code we'd use much more compact code without all those comments, and with related commands together not one per line.

Defining Functions

To define functions we need to do two things, pick up a namespace, and the declare function together with what top of the stack looks before and after. The stack state is heavily annotated as well.

#!/Applications/factor/factorUSING: io math math.parser kernel ;IN: double: double-number ( n -- m ) 2 * ;200[                ! 200  dup            ! 200 200  double-number  ! 200 400  number>string  ! 200 "400"  print          ! 200  1              ! 200 1  +              ! 201  dup            ! 201 201  210            ! 201 201 210  <=             ! 201 t]loopdrop
$ ./double.factor400402404406408410412414416418420

Fibonacci

#!/Applications/factor/factorUSING: io math math.parser kernel ;IN: fib: fib ( n -- m )  dup          ! N N  2 <=         ! N t/f  ! called if <= 2  [             ! N    drop        ! empty    1           ! 1  ]  ! called if > 2  [             ! N    dup         ! N N    1 -         ! N N-1    fib         ! N fib(N-1)    swap        ! fib(N-1) N    2 -         ! fib(N-1) N-2    fib         ! fib(N-1) fib(N-2)    +           ! fib(N-1) + fib(N-2)  ]  if;! does not remove top of the stack: print-fib ( n -- n )  "fib(" write  dup number>string write  ") = " write  dup fib number>string write  "
" write;1[ print-fib 1 + dup 20 <=]loopdrop
$ ./fib.factorfib(1) = 1fib(2) = 1fib(3) = 2fib(4) = 3fib(5) = 5fib(6) = 8fib(7) = 13fib(8) = 21fib(9) = 34fib(10) = 55fib(11) = 89fib(12) = 144fib(13) = 233fib(14) = 377fib(15) = 610fib(16) = 987fib(17) = 1597fib(18) = 2584fib(19) = 4181fib(20) = 6765

It's pretty much what you'd expect from a stack-based language. write is like print except it doesn't append newline. It's also in io library. I annotated stack state for the interesting function.

FizzBuzz

Here's highly annotated FizzBuzz:

#!/Applications/factor/factorUSING: io math math.parser kernel ;IN: fizzbuzz: is-fizz ( n -- t/f )  3       ! N 3  mod     ! N%3  0       ! N%3 0  =       ! t/f;: is-buzz ( n -- t/f )  5       ! N 5  mod     ! N%5  0       ! N%5 0  =       ! t/f;: fizzbuzz ( n -- str )  dup                 ! N N  is-fizz             ! N isFizz  [                   ! N    is-buzz           ! isBuzz    [ "FizzBuzz" ]    ! "FizzBuzz"    [ "Fizz" ]        ! "Fizz"    if  ]  [    dup               ! N N    is-buzz           ! N isBuzz    [ drop "Buzz" ]   ! "Buzz"    [ number>string ] ! "N"    if  ]  if;! does not remove top of the stack: print-fizzbuzz ( n -- n )  dup fizzbuzz print;1[  print-fizzbuzz  1 +  dup 100 <=]loopdrop

Should you use Factor?

No.

The import system is ridiculously bad, and if you want to just play with the language casually, it overwhelms any good aspects of the language.

If you're really serious and willing to memorize all the imports, that could work, but there's really not that much of a reward at the end. It's just another stack based language, with nothing special about it, and no clear use case.

For people who want to pick a stack based language to play with, I'd recommend Postscript or one of the esoteric ones like Befunge to maximize the fun. None of them are good for serious use.

Unlike most other stack-based languages which trust you to get the stack right, Factor requires stack effects annotations, and will not compile if you get them wrong, in a sort of a "type system". This is arguably helpful, but the error messages you get are truly awful. Which is also typical of most type systems.

Code

All code examples for the series will be in this repository.

Code for the Factor episode is available here.


Original Link: https://dev.to/taw/100-languages-speedrun-episode-71-factor-3bb2

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To