The Scheme Programming Language, Third Edition [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

The Scheme Programming Language, Third Edition [Electronic resources] - نسخه متنی

Jean-Pierre Hbert, R. Kent Dybvig

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید





4.5. Assignment











(set! var exp)


syntax


returns: unspecified


set! does not establish a new binding for var but rather alters the value of an existing binding. It first evaluates exp, then assigns var to the value of exp. Any subsequent reference to var within the scope of the altered binding evaluates to the new value.

It is an error to assign a top-level variable that has not yet been defined, although many implementations do not enforce this restriction.

Assignments are not employed as frequently in Scheme as in most traditional languages, but they are useful for implementing state changes.


(define flip-flop
(let ((state #f))
(lambda ()
(set! state (not state))
state)))
(flip-flop) ⇒ #t
(flip-flop) ⇒ #f
(flip-flop) ⇒ #t

page 66).


(define memoize
(lambda (proc)
(let ((cache '()))
(lambda (x)
(cond
((assq x cache) => cdr)
(else (let ((ans (proc x)))
(set! cache (cons (cons x ans) cache))
ans)))))))
(define fibonacci
(memoize
(lambda (n)
(if (< n 2)
1
(+ (fibonacci (- n 1)) (fibonacci (- n 2)))))))
(fibonacci 100) ⇒ 573147844013817084101

/ 98