Skip to content

Instantly share code, notes, and snippets.

@alexandream
Created April 22, 2012 20:21
Show Gist options
  • Save alexandream/2466632 to your computer and use it in GitHub Desktop.
Save alexandream/2466632 to your computer and use it in GitHub Desktop.
Print 1 to 100 without loops or conditionals
;; This creates a function that will print n and increment it on every call.
(define (make-printer)
(let ((n 1))
(lambda ()
(display n)
(newline)
(set! n (+ n 1)))))
;; The "add" function returns the function that executes the parameters f and g
;; one after the other.
;;
;; The "times-N" functions return functions that execute their parameter N
;; times in sequence.
(define (add f g) (lambda () (f) (g)))
(define (times-2 f) (add f f))
(define (times-5 f) (add (times-2 (times-2 f)) f))
(define (times-10 f) (times-2 (times-5 f)))
;; Run the solution for this "problem".
((times-10 (times-10 (make-printer))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment