;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "reader.ss" "plai" "lang")
;; Define is kind of an imperative kluge that we use at "top level":
(define g 9.81)
(define t 2)
(define v 0)
;; final velocity is:
(+ v (* t (* g g)))
;; We can write this in a purely functional style with LET:
(let ([g 9.81]
[t 2]
[v 0])
(+ v (* t (* g g))))
;; We can also bind procedure values using LET, of course:
(let ([is-even? (lambda (x) (equal? 0 (remainder x 2)))])
(or (is-even? 7) (is-even? 3)))
;; Although we get into a problem with recursive procedures:
;(let ([is-even? (lambda (x) (equal? 0 (remainder x 2)))]
; [is-odd? (lambda (x) (not (is-even? x)))])
; (or (is-even? 7) (is-odd? 3)))
;
;(let ([factorial (lambda (x) (if (<= x 1) 1 (* x (factorial (- x 1)))))])
; (factorial 7))
; The solution is LETREC, which allows the values on the
; left to appear on the right:
(letrec ([factorial (lambda (x) (if (<= x 1) 1 (* x (factorial (- x 1)))))])
(factorial 7))