This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts MenuA direct translation of the Scheme code to newLisp might be:Quote from: "Sammo"
; make-square
; 2011-03-05
; example
; (set 's1 (make-square 2))
; (s1 'area) ;result = 4
; (s1 'perimeter) ;result = 8
; (s1 'xxx) ;result = "unknown message" error
(define (make-square side)
(letex (s side)
(lambda (msg)
(cond
((= msg 'area) (mul s s))
((= msg 'perimeter) (mul 4 s))
(true (throw-error "unknown message")) ))))
(define (make-square side)
(lambda (msg)
(cond ((eq? msg 'area) (* side side))
((eq? msg 'perimeter) (* 4 side))
(else (error "unknown message")))))
> (define s1 (make-square 4))
> (s1 'area)
> 16