Imperative Programming
More I/O
write: similar to display but prints objects in
their standard literal representation.
>
(begin
(write "this is a string")
(display "this is a string ")
(write #\x)
(display #\space)
(display #\x)
(display #\newline))
> "this is a string"this is a string #\x x
readchar: reads one character at a time
read : reads one datum at a time
eofl-object?: returns true (#t) if at end of file
for either read or readchar. Returns #f otherwise
Most scheme/Lisp implementations also provide
eval: takes a value whose printed representation
should be a scheme expression and returns the
value of this expression.
> (eval '(+ 1 2))
3
> (eval '(cons 1 '(a b)))
(1 a b)
>
Implement a read-eval-print loop (repl)
(define repl
(lambda ()
(display "--> ")
(write (eval (read)))
(newline)
(repl)))
> (repl)
--> (+ 1 2)
3
--> (car (cons "foo" 'foo))
"foo"
--> (cons "foo" 'foo)
("foo" . foo)
--> (cdr 3)
Error in cdr: 3 is not a pair.
Type (debug) to enter the debugger.
>
Errors cause return to scheme's repl
eval is convenient to implement repls for other
languages
Data structure Mutations
set-car!
set-cdr!
(> (define lst (list 1 2))
> (set-car! lst 3)
> lst
(3 2)
> (set-cdr! lst (list 4 5))
> lst
(3 4 5)
> (set-cdr! lst 6)
> lst
(3 . 6)
>
(define p (cons 1 '()))
> (begin (set-cdr! p p) 'done)
done
> p
???
Mutable:
Data elements that may be changed.
Pairs created by cons, list
Vectors: created by vector
> (vector 1 2 3 4)
#4(1 2 3 4)
Strings: created by string
> (string #\h #\e #\l #\l #\o)
"hello"
>
Immutable:
symbols and numbers
created by ' (quote ...)
Assignment:
Variables have been introduced as bound directly
to VALUES
for assignment
VARIABLES ARE BOUND TO LOCATIONS
Variable reference now refer to values at these
LOCATIONS
(set! var exp)
evaluates exp and places the resulting value in
the location that is the binding of variable var
Procedures that do assignment operations usually
end in ! (bang) in scheme.
bang signals danger since it is harder to reason
about such procedures.
> (define x 1)
> (set! x 2)
> x
2
> (let ((y 3))
(set! y 4)
(+ y 1))
5
>
If a GLOBAL binding does not exist for a variable
one must be created using define before assigning
to it using set!
The first meaningful value for a binding may be an
assignment that is not at the top-level, in that
case we initially bind the variable to some
arbitrary value
> (define a '*)
> (let ((b 'not-three))
(displayln "b is " b)
(set! a 3)
(if (= a 3) (set! b 'three) )
b)
b is not-three
three
>
> (define empty? '*)
> (define push! '*)
> (define pop! '*)
> (define top '*)
> (let ((stk '()))
(set! empty?
(lambda () (null? stk)))
(set! push! (lambda (x)
(set! stk (cons x stk))))
(set! pop! (lambda ()
(if (empty?)
(error 'pop "Stack is empty")
(set! stk (cdr stk)))))
(set! top (lambda ()
(if (empty?)
(error 'top "Stack is empty")
(car stk)))))
> (push! 1)
> (push! 2)
> (top)
2
> (pop!)
> (top)
1
> (pop!)
the stack procedures share the same stk binding.
Because this binding is not accessible outside the
let, the stack is a data abstraction.
(set! push! (lambda (x)
(set! stk (cons x stk))))
Q: What does push! return
A: what does set! return
if set! returns the stk then we would know the
representation and perhaps modify it.-->Flaw
Fix: modify push! to return 'done or x
(define stack
(let ([stk '()])
(lambda (message)
(case message
((empty?) (lambda ()
(null? stk)))
((push!) (lambda (x)
(set! stk (cons x stk))))
((pop!) (lambda ()
(if (null? stk)
(error 'pop! "stack empty")
(set! stk (cdr stk)))))
((top) (lambda ()
(if (null? stk)
(error 'top "stack empty")
(car stk))))
(else (error 'stack "invalid message"))))))
> ((stack 'push!) 1)
> ((stack 'push!) 2)
> ((stack 'top))
2
> ((stack 'pop!))
> ((stack 'top))
1
> ((stack 'pop!))
> ((stack 'top))
Error in top: stack empty.
Type (debug) to enter the debugger.
Multiple stacks
(define make-stack
(lambda ()
.....))
Page 130 in book.
If a procedure has bindings that have been
modified by assignment or mutation
it is said to have STATE
Objects: when message passing is used to access
state these procedures are called objects
When combined with inheritance, the resulting
style is called OOP.
Sushil Louis
Last modified: Mon Nov 1 10:29:34 PST 1999