CS代考 COMP1811 Paradigms of Programming

COMP1811 Paradigms of Programming
Side effects in Scheme
set!, begin, for-each, ports

Copyright By PowCoder代写 加微信 powcoder

• conventions on s-expressions
• void statement
• assignment
• sequencing
• input/output
• APPENDIX: Sample codes

• Scheme allows side effects (see prev. lecture)
• From now on, we call a S-Expression :
– Expression: No side effects
• Immutablesymbols,algebraicoperations(+,length…)
• It yields values
– Statement: Side effects
• Accessing/modifyingmutablesymbols,sequencing,ports..
• It may subsume Expressions as part of them.
• It may yield value or not.
Conventions

• void is the most basic statement ever. – it makes nothing, returns nothing
• induces a new value/type for statements returning nothing: void.

• set!overwrites(mutates)the content of a symbol.
(set! symbol exp1)
• set! is a predefined symbol/word
• symbol is a defined symbol
• exp can be
• either atom
• or S-Expression
Unlike Python, symbol must exists before update!
Assignment

• for-each allows as to iterate an statement on a list.
for-each is a predefined symbol/word
statement is a procedure of arity n (as in case of variant map). usually is given in lambda form. list1..listn are lists
(for-each statement list1…listn)

• MIND THE DIFFERENCE :
– maps cares about the returned
value (even void) • return a list
– for-each cares only about side effect (forgets value)
• return void
(didactic only: maps should never take statements)
for-each vs map

• Side effects induces sequencing (order matters)
• Begin forces statement execution in order of
appearance.
• It yields the return value of the last S-Expression
• begin is a predefined symbol/word
• exp1…expn are
• either atom
• or S-Expression
(begin exp1 exp2 … expn)
Sequencing

• Input-output actions are inherently side-effects.
– display yields output to the current terminal (mind
– read from current terminal as if you entered in the REPL
display is a predefined
symbol/word
(display exp1)
• eitheratom
• or S-Expression
Mind return type. Can read any valid Scheme type: bool, char, symbol, even lists!!
Input-output
display, read

• display, read can be given ports (handlers)
– FileSystem,
– Network Connection,
– Data base connections
• As usual sequencing protocol is
– open descriptor
– display/read
– close descriptor
Together with recursion (see theme) provides access to entire files.
Input-output

On defining functions in Scheme
1. Every statement, (either simple or compound) will be enclosed with a begin block (sequencing)
2. Expressions cannot take statements as parameters, unless enclosed themselves by begin block.
Scheme is (Python like) dynamically typed.
Proceeding this way, programmer is aware of side effects (avoiding equality problems)
(Inspired by Haskell monad-trapping)
* This is not strict. A certain license by your module leader can be given.
Progamming style.
The COMP1811 Guidance*

* This is not strict. A certain license by your module leader can be given.
Side-effect samples*

This is not strict. A certain license by your module leader can be given.
Side-effect samples*

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