logoalt Hacker News

anthkyesterday at 9:18 PM3 repliesview on HN

I tried some ML language once, it's difficult even to write a basic factorial example, which in Scheme I could do it iteratively and recursively with ease.

Either with S9 Scheme for quick fun (it has Unix sockets and ncurses :D ) or Chicken Scheme for completeneless (R5RS/R7RS-small + modules), I always have fun with both.

Oh, and well, Forth, too, but more like a puzzle (altough it shines to teach you that you can do a lot with a fixed point). Hint: write helpers for rationals -a/b where a is an integer and b a non-zero integer- and complex numbers by placing two items in the stack for each case (for rat helpers you need four (a/b [+-*/] c/d) .

You can have a look at qcomplex.tcl (either online or installed) as an example on how can it work even under JimTCL itself by just sourcing that file. Magic, complex numbers under jimsh thanks to the algebraic properties. So, you can implement the same for yourself in some Forths, even under EForth for Muxleq. Useless? It depends, under an ESP32 it can be damn fast, faster than Micropython.


Replies

zelphirkaltyesterday at 10:32 PM

From my limited SMLNJ experience I think for something as simple as factorial, it is nearly the same. Both have TCO, recursion, inner functions, pattern matching and those good things. You can structure the code the same way.

zarakshRyesterday at 9:33 PM

I don't see how:

Racket:

  > (define (fact n)
      (if (= n 1)
          1
          (* n (fact (- n 1)))))
  > (fact 6)
  720
OCaml:

  # let rec fact = function
      | 1 -> 1
      | n when n > 1 -> n * (fact (n - 1))
    in fact 6;;
  - : int = 720
show 1 reply
nine_kyesterday at 9:36 PM

Even as simple as

  fac 1 = 1
  fac n = n * (fac (n - 1))
which is a working Haskell implementation?

I mean, in Scheme it is longer to write. I enjoy Lisps and use Emacs for everything, but Haskell can be as terse, or even more terse. (Which is not always a good thing.)