Loop without Loops

Started by newdep, April 20, 2011, 11:29:22 AM

Previous topic - Next topic

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).

 

(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 ?
-- (define? (Cornflakes))

Kazimir Majorinc

#1
(eval (setf code '(when (< counter 40)
                    (inc counter)
                    (println counter)
                    (eval code))))


or even better, "crawler-tractor"

                 
(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,


(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">http://www.newlisp.org/index.cgi?Y_Function
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

newdep

#2
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!....
-- (define? (Cornflakes))

newdep

#3
Mind set to the right frequency and it flows again...

..more on map..

# 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

>
-- (define? (Cornflakes))