CS代考 COMP1811 Paradigms of Programming

COMP1811 Paradigms of Programming
Notes on Metaprogramming in Scheme
quote, eval, apply, define

Copyright By PowCoder代写 加微信 powcoder

• Notes on metaprogramming.
– Object Language and MetaLanguage. – Intercepting evaluation process.
– quote, symbol?
– eval, apply, define
• Symbolic computation
– Dealing with unresolved symbols
• Lists/Metaprogramming
– A handy notation for lists.

• Some concepts presented are unusually hard.
• Do not struggle too much.
• In a hurry:
– Just pick practical issues (see last slide)
– No need to understand to use (it may help, though)
homoiconic languages

• An object language is used to describe the world
The card is blue
• A metalanguage is used to describe the language,
The statement “The card is blue” is enunciated in English
NOTE: Mind the quotes … They will become relevant later
• A truck carries goods
• A language operates on
Object Language/Meta language

• Recap on S-Expressions
– Atoms: 1,2,#f,#t, symbols
– Non-atoms: (s_1,…,s_n)
• The evaluation mechanism is as follows.
– A reader layer turns sequence of
chars into atoms, lists.
– An expander layer evaluates the
atoms, lists.
• From object language, you can at will
– freeze expander layer at will
– reactivate expander layer (evaluate …)
S-Expressions
Intercepting evaluation process

– Atoms (1,#f,id1…) are frozen
• No effect on constant literals.
• Anything else (ids) frozen into
– Non-atoms, (s_1,…,s_n)
• lists of frozen S-Expressions
• symbols:
– They may not hold any value
– They are values themselves.
– Symbol are NOT variables.
Effects on S-Expressions

– Atoms (literals, symbols) are
interpreted
• No effect on constant literals
• Symbols as procedures.
– Non-atoms, (s_1,…,s_n)
• Evaluates each the list items
• Then applies first to the rest.
– Takes a procedure and a list of values.
It applies then former to latter.
– Gives an interpretation to the symbol
– It is NOT exactly an assignment.
eval, apply, define
Effects on S-Expressions

• Scheme/LISP is homoiconic
– Language itself as data (meta-
programming)
– Symbols as values themselves.
– Easier symbolic computation
• Remember those old days learning polynomials ?
– You deal with symbols, a,b,c,x… not holding any concrete value!
– Harder with non-homoiconic languages (python)
• Let’s give a chance to symbolic computation!
𝑃𝑥 =𝑎𝑥2+𝑏𝑥+𝑐 𝑄𝑥 =𝑎′𝑥2+𝑏′𝑥+𝑐′
𝑃 𝑥 +𝑄 𝑥 =(𝑎+𝑎′)𝑥2 +(𝑏+𝑏′)𝑥+(𝑐+𝑐′)
Symbolic Computation

• quote admits shortcut notation:
– (quote (1 2 3)) as ‘(1 2 3)
– (quote (1 (#f #t)) as ‘(1 (#f #t))
• What is the point about metaprogramming/lists?
– quote returns lists of S- Expressions.
– Symbols inside don’t need to be escaped!
• Shortcut for quote is a handy notation for lists…
Lists and Metaprogramming
Handy notation for lists

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com