lambda.scm 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. (import
  2. (rename
  3. (only
  4. (scheme base)
  5. define lambda if let +)
  6. (if scheme-if)
  7. (+ scheme-+))
  8. (scheme read)
  9. (scheme write))
  10. (define (decode-boolean b)
  11. ((b #t) #f))
  12. (define (encode-boolean b)
  13. (scheme-if b true false))
  14. (define (decode-number n)
  15. ((n (lambda (x) (scheme-+ x 1))) 0))
  16. (define (encode-number n)
  17. (let loop ((i 0)
  18. (result zero))
  19. (scheme-if (< i n)
  20. (loop (scheme-+ i 1)
  21. (succ result))
  22. result)))
  23. (define false (lambda (a) (lambda (b) b)))
  24. (define zero (lambda (a) (lambda (b) b)))
  25. (define null (lambda (a) (lambda (b) b)))
  26. (define not (lambda (pred) (lambda (a) (lambda (b) ((pred b) a)))))
  27. (define true (lambda (a) (lambda (b) a)))
  28. (define succ (lambda (n) (lambda (f) (lambda (x) (f ((n f) x))))))
  29. (define cons (lambda (a) (lambda (b) (lambda (c) ((c a) b)))))
  30. (define car (lambda (p) (p true)))
  31. (define cdr (lambda (p) (p false)))
  32. (define or (lambda (a) (lambda (b) ((a true) b))))
  33. (define + (lambda (a) (lambda (b) ((b succ) a))))
  34. (define zero? (lambda (n) ((n (lambda (x) false)) true)))
  35. (define null? (lambda (p) ((p (lambda (a) (lambda (b) (lambda (c) false)))) true)))
  36. (define pred
  37. (lambda (n)
  38. (lambda (f)
  39. (lambda (x)
  40. (cdr
  41. ((n (lambda (p)
  42. ((cons
  43. false)
  44. (((car p)
  45. (cdr p))
  46. (f (cdr p))))))
  47. ((cons
  48. true)
  49. x)))))))
  50. (define - (lambda (a) (lambda (b) ((b pred) a))))