;; Welcome to the simplisp.js worksheet
;; Hit Ctrl-Enter to execute everything
;; atoms
(define answer 42)
answer
;; functions
(define sum-curry
(lambda (x)
(lambda (y)
(+ x y))))
((sum-curry 1) 2)
(define sum-normal
(lambda (x y)
(+ x y)))
(sum-normal 1 2)
(define (sum-sugar x y)
(+ x y))
(sum-sugar 1 2)
;; factorial examples
(define (fact-naive n)
(if (< n 2)
1
(* n (fact-naive (- n 1)))))
(fact-naive 5)
(define (fact-tail n)
(define (fact-helper num acc)
(if (< num 2)
acc
(fact-helper (- num 1) (* acc num))))
(fact-helper n 1))
(fact-tail 5) ;; no TCO yet
(define (fact-sugar n)
(let fact-helper ((num n) (acc 1))
(if (< num 2)
acc
(fact-helper (- num 1) (* acc num)))))
;(fact-sugar 5) ;; no named let yet
;; sqrt example
(define (square x) (* x x))
(define (abs x)
(if (< x 0)
(- x)
x))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (sqrt x)
(sqrt-iter 1.0 x))
(sqrt 9)
(sqrt (+ 100 37))
(sqrt (+ (sqrt 2) (sqrt 3)))
(square (sqrt 1000))
;; halting problem
(define (halts? program input)
;; million dollar logic goes here
)
(define (run-forever) (run-forever))
(define (try program)
(if (halts? program program)
(run-forever)
(quote halted)))
(try try) ;; what now?