Introduc)on ¡to ¡Racket, ¡a ¡dialect ¡of ¡LISP: ¡ Expressions ¡and ¡Bindings ¡
CS251 ¡Programming ¡Languages ¡
Spring ¡2016, ¡Lyn ¡Turbak ¡
¡
Department ¡of ¡Computer ¡Science ¡ Wellesley ¡College ¡
Introduc)on to Racket, a dialect of LISP: Expressions and - - PowerPoint PPT Presentation
Introduc)on to Racket, a dialect of LISP: Expressions and Bindings CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science
¡
Department ¡of ¡Computer ¡Science ¡ Wellesley ¡College ¡
3-‑2 ¡
3-‑3 ¡
i.e., ¡not ¡just ¡number ¡crunching ¡ Emacs: ¡M-‑x ¡doctor ¡
3-‑4 ¡
– Lambda ¡the ¡Ul)mate ¡PL ¡blog ¡inspired ¡by ¡these: ¡ hap://lambda-‑the-‑ul)mate.org ¡
3-‑5 ¡
– Some ¡changes/improvements, ¡quite ¡similar ¡
3-‑6 ¡
3-‑7 ¡
3-‑8 ¡
3-‑9 ¡
3-‑10 ¡
¡
3-‑11 ¡
¡
3-‑12 ¡
(+ 3 (+ 5 4)) ↓ ¡12 by ¡the ¡addi)on ¡rule ¡because: ¡
– 5 ↓ ¡5 by ¡the ¡value ¡rule ¡ – 4 ↓ ¡4 by ¡the ¡value ¡rule ¡ – 5 ¡and ¡4 ¡are ¡both ¡numbers ¡ – 9 ¡is ¡the ¡sum ¡of ¡5 ¡and ¡4 ¡ ¡
3-‑13 ¡
¡
¡
¡
3-‑14 ¡
where ¡v ¡is ¡a ¡value ¡ (number, ¡boolean, ¡etc.) ¡ ¡
¡
¡
¡
Where ¡v1 ¡and ¡v2 ¡are ¡numbers ¡and ¡ ¡v ¡is ¡the ¡sum ¡of ¡ ¡v1 ¡and ¡v2. ¡
¡
side ¡condi)ons ¡of ¡rules ¡
3-‑15 ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡5 ↓ ¡5 (value) ¡ 4 ↓ ¡4 (value) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡(+ 5 4) ¡↓ ¡ ¡9
¡
¡
¡
(addi)on) ¡ Stuck ¡here. ¡Can’t ¡apply ¡ (addi)on) ¡rule ¡because ¡ ¡ #t ¡is ¡not ¡a ¡number ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡5 ↓ ¡5 (value) ¡ #f ↓ ¡#f (value) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
¡
¡
Stuck ¡here. ¡Can’t ¡apply ¡ (addi)on) ¡rule ¡because ¡ ¡ #f ¡is ¡not ¡a ¡number ¡
3-‑16 ¡
¡
3-‑17 ¡
3-‑18 ¡
¡
¡
¡
Where ¡v1 ¡and ¡v2 ¡are ¡numbers ¡and ¡ ¡v ¡is ¡#t ¡if ¡v1 ¡is ¡less ¡than ¡v2 ¡ ¡
3-‑19 ¡
3-‑20 ¡
¡
¡
¡
where ¡v1 ¡is ¡not ¡#f ¡
¡
¡
¡
3-‑21 ¡
3-‑22 ¡
3-‑23 ¡
3-‑24 ¡
id1 → v1, id2 → v2, …, idn → vn
3-‑25 ¡
3-‑26 ¡
bindings ¡to ¡the ¡least-‑recently ¡added ¡bindings ¡(front ¡to ¡back ¡in ¡our ¡ representa)on) ¡
and ¡true ¡evaluates ¡to ¡#t ¡
3-‑27 ¡
3-‑28 ¡
env0 ¡= ¡∅
env1 ¡= ¡x→ 3, ∅ ¡(abbreviated ¡x→ 3, ¡can ¡write ¡as ¡x -> 3, . ¡in ¡text) ¡ ¡
env2 ¡= ¡y → 12, x → 3 (most ¡recent ¡binding ¡2irst)
env3 ¡= ¡diff → 9, y → 12, x → 3
env4 ¡= ¡test → #t, diff → 9, y → 12, x → 3
3-‑29 ¡
¡
¡
¡
3-‑30 ¡
where ¡v ¡is ¡a ¡value ¡ (number, ¡boolean, ¡etc.) ¡ ¡ (value) ¡
¡
¡
¡
(addi)on) ¡ Where ¡v1 ¡and ¡v2 ¡are ¡numbers ¡and ¡ v ¡is ¡the ¡sum ¡of ¡ ¡v1 ¡and ¡v2. ¡
¡
¡
¡
where ¡id ¡is ¡an ¡iden)fier ¡and ¡ id ¡→ v ¡is ¡the ¡first ¡binding ¡in ¡ ¡ env ¡for ¡id ¡ ¡ (varref) ¡
¡
¡
(if ¡nonfalse) ¡ where ¡v1 ¡is ¡not ¡#f
¡
(if ¡false) ¡
Only ¡this ¡rule ¡actually ¡ ¡ uses ¡env; ¡others ¡just ¡ pas ¡it ¡along ¡
3-‑31 ¡
¡
¡
3-‑32 ¡
– Can’t ¡contain ¡whitespace ¡ – Can’t ¡contain ¡special ¡characters ¡()[]{}”,’`;#|\ – Can’t ¡have ¡same ¡syntax ¡as ¡a ¡number ¡ ¡
– myLongName, my_long__name, my-long-name ¡ ¡ – is_a+b<c*d-e? – 76Trombones
3-‑33 ¡
3-‑34 ¡
3-‑35 ¡
3-‑36 ¡