Created
August 23, 2011 12:10
-
-
Save mnicky/1164956 to your computer and use it in GitHub Desktop.
How to make an anonymous function call itself
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;we define a function 'starter' which calls its first parameter on itself and the second parameter | |
(define starter | |
(lambda (f arg) | |
((f f arg)))) | |
;we call the 'starter' on anonymous (lambda) function (calling its first parameter on itself and the second parameter) and a number | |
(starter (lambda (g num) | |
(printf "~a\n" num) | |
(g g (+ num 1))) | |
0) | |
;the lambda will call itself and will run forewer (with tail call optimalization), printing ascending numbers | |
;...and calling it all together at the point of definition | |
((lambda (f arg) | |
((f f arg))) | |
(lambda (g num) | |
(printf "~a\n" num) | |
(g g (+ num 1))) | |
0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can u explain it plz