COMP1811 Paradigms of Programming
Introduction to Scheme-Syntax
S-Expressions
Copyright By PowCoder代写 加微信 powcoder
• S-Expressions and prefix form
• How to form S-Expressions
• Atomic S-Expressions
• Compound S-Expressions – Calling functions
– Conditional Expressions
– Defining my own symbols
• Constants • Functions
• function application in prefix form:
– Idea: first the symbol, then the arguments
• S-Expressions (Symbolic-Expressions) are about that.
Traditional (infix form)
S-Expression (Prefix form)
(3*(2+4))/12
(/ (* 3 (+ 2 4) 12)
f*sin(o*t+w)
(* f (sin (+ (* o t) w)))
S-Expressions
Inductively defined
– Single atoms (1, 2, “Hello”, #f ,#t, 2.3,+,-…)
– (s_1 , … , s_n) being s_i an S-expression
The atoms are the “smallest” dolls
Every pair of circle brackets define a containing Russian doll
The way you are used to:
(3+4) * (2/1) = 14
A containing doll can be contained in turn
(* (+34) (/21))
(=(* (+34) (/21))14)
S-Expressions
#f,#t,2,3,4,”Hello”,+…
Scheme REPL (Read-Eval-Print-Loop)
evaluates expressions on pressing RUN
• Booleans • Numbers • Chars
• Symbols!
(S-Expressions. Atoms)
(The nested boxes evoke Russian dolls)
(id param_1 param_2…param_n)
• id is a predefined symbol
• +,/,*,quotient, modulo…
• abs, sqrt,>=
• string-length, string-
• param_i can be • Either atoms • or compound
(S-Expressions. Function calls I)
(id param_1 param_2…param_n)
• Though valid syntactically, some S-expressions may have no sense…
• See the REPL message to understand why…
• It is very important to detect errors in your code.
(S-Expressions. Function calls II)
(The nested boxes evoke Russian dolls)
(if bool exp1 exp2)
• if is a predefined symbol/word
• bool is a Boolean expression
• exp1 returned when bool is
• exp2 return when bool false
• bool, exp1,exp2 can be either • Eitheratom…
• or compound
(S-Expressions: Conditional Expressions)
[ bool1 exp1], [ bool2 exp2 ], [ bool3 exp3 ], …
[ else exp3] )
(The nested boxes evoke Russian dolls)
• cond is a predefined symbol/word
• The first branch whose bool becomes true return exp
• Bool, exp can be
• either atom
• or compound
S-Expressions: Multi-cond Expressions
(define id exp)
• define is a predefined symbol/word
• id is an identifier
• exp can be
• either atom
• or compound
S-Expressions: defining symbols (constants)
(define (id p1…pn) exp1)
• define is a predefined symbol/word
• id is an identifier
• p1…pn are identifiers
(parameters…)
• exp can be
• either atom
• or compound
S-Expressions: defining symbols (functions)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com