Page 1 of 1

Loop without Loops

Posted: Wed Apr 20, 2011 6:29 pm
by newdep
I was wondering how many functions inside newlisp are useable to a create a functional loop with.
So.. thats without the use of 'until 'while 'for (the existing repeat/loop functions)..etc..

Here a very basic example
functions themselfs can loop i.e. running either of these will return the looped function of (print y).

Code: Select all

(define (loop x) (or (zero? x) (go x)) )
(define (go y) (print y) (loop (dec y)))

(go 10)
or
(loop 10)
Now im wondering what's more to use? I would love to see a 'map or 'apply functional loop without
the use of the pre-defined loop/repeat functions in it.
...did not figure it out yet ;-) Anyone ?

Re: Loop without Loops

Posted: Wed Apr 20, 2011 7:57 pm
by Kazimir Majorinc

Code: Select all

(eval (setf code '(when (< counter 40)
                    (inc counter)
                    (println counter)
                    (eval code))))
or even better, "crawler-tractor"

Code: Select all

(set 'f (lambda()
            (if (< counter2 50)
                (begin (println "Hi for the " (inc counter2) ". time. ")
                          (push (last f) f -1)
                          (if (> (length f) 3) (pop f 1))))))

(f) 
This last is particularly charmy because it doesn't use recursion and Lutz actually made some change so it doesn't result in stack overflow at all.

I tried to do same with map,

Code: Select all

(setf L '(1 2))
(map (lambda(x)(when (< x 10)
                     (push (+ x 2) L -1))
               (println L))
     L)
but it doesn't work, i.e. original version of L is used, not modified version, perhaps some optimization. I guess that other functions are optimized on the same way. One can always use (sequence 1 50), but it looks too trivial, I think.

Finally, Lutz implemented Y-combinator from lambda-calculus in Newlisp:

http://www.newlisp.org/index.cgi?Y_Function

Re: Loop without Loops

Posted: Wed Apr 20, 2011 9:04 pm
by newdep
ahaaaaa...I thought you like this topic and i was almost certain you had your mind set to it already...Yes i remembered your crawer-tractor... realy nice ! I was close in finalizing them almost..almost near these examples you just post here, but i could not get my mind to finish it ..1 level too deep..;-) These are actualy very nice brain crackers! Well done.. love them... I missed out on the Y-combinator Ill study that Thanks!....

Re: Loop without Loops

Posted: Thu Apr 21, 2011 7:18 am
by newdep
Mind set to the right frequency and it flows again...
..more on map..

Code: Select all

# loops y * 1-second
(define (loop y)
        (map (fn(x) (sleep (mul x 1000)) (println (++ y))  ) (dup 1 y))  )

#loops backwards from y to z
(setf loop (lambda(y z)
      (map (fn(x) (println (dup "@" x) )) (sequence y z))
           true ))

#loops backwards to 1
(setf loop (lambda(y)
      (map (fn(x) (println (dup "@" x) )) (sequence y 1))
           true ))
> (loop 10)
@@@@@@@@@@
@@@@@@@@@
@@@@@@@@
@@@@@@@
@@@@@@
@@@@@
@@@@
@@@
@@
@
true
>