LTNS, actually... I'm unrusting myself with an interesting page I found, essays in J language ( http://www.jsoftware.com/jwiki/Essays ).
I am trying the first one, the Collatz conjecture, http://www.jsoftware.com/jwiki/Essays/C ... Conjecture , and I was wondering how do you do in idiomatic newlisp the kind of collect that's displayed there, which is a kind of fold operation:
"apply function recursively and accumulate the results until it returns nil..."
"
I haven't really thought a lot about it yet, and my first attempt, which I offer below, is pretty naive, although it works...
anyone up to discuss about this?
thanks!
PS- the code:
Code: Select all
(context 'collatz)
(define (collatz1 n)
(if (<= n 1)
nil
(if (= (mod n 2) 0)
(/ n 2)
(+ 1 (* 3 n)) ) ) )
(define (collatz:collatz n)
(let ((ret '()))
(do-while (> n 1)
(setq n (collatz1 n))
(push n ret -1) )
ret ) )
(context 'MAIN)
> (collatz:collatz 9)
(28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1)