Академический Документы
Профессиональный Документы
Культура Документы
System Exercise
Figure 44 in HtDP
Racket
Primi;ves
Data
deni;on
1. (define-struct 2. (define-struct file (name size content)) dir (name dirs files))
Note, dene-struct is not available in Legacy Languages => (Scheme) R5RS. So, choose the language level as Teaching Languages =>HtPD => Intermediate-level or Advanced.
Addi;onal
Issues
Do
not
need
teach
packs
(e.g.,
dir.ss)
for
this
assignment
because
naviga;ng
through
real
Windows/UNIX
directories
not
required.
Use
secure
shell
ssh
to
log
into
unixapps1
and
s,p
to
copy
les
into
unixapps1
before
execu;ng
(one
of
the
following)
turnin
command.
/common/public/tkprasad/cs480/turnin-pa1
leSys.scm
/common/public/tkprasad/cs480/turnin-pa1
leSys.rkt
Func;on
Deni;ons
Represent
gure
as
EG44
Dene
how-many
:
Number
of
les
Dene
du-dir
:
Disk
usage
(size)
Dene nd? : Determines if a le occurs in the directory tree CS680 Only Dene nd : Determines list of le paths if there are mul;ple occurrences of a le in the directory tree
Submit the version in which both the directory and the le require 1 unit of storage for holding their meta-informa;on, and addi;onally, each le requires storage to hold its content, whose size has been explicitly specied.
Remove-All
(define (rm-all sym ll) (cond ( (null? ll) '()) ( (symbol? (car ll)) (if (eq? sym (car ll)) ( rm-all sym (cdr ll) ) ( cons (car ll) (rm-all sym (cdr ll)) ) ) ) (else (cons (rm-all sym (car ll)) (rm-all sym (cdr ll)) ) ) ) )
Write a recursive definition for a function ops that counts the number of + s.
cs480(Prasad) L14Recur 10
(define (ops e) ; e is assumed to be a symbol or a list (cond ((symbol? e) 0) ((eq? (car e) '+) (+ 1 (ops (cadr e)) (ops (caddr e))) ) ((eq? (car e) 'if) (+ (ops (cadr e)) (ops (caddr e)) (ops (cadddr e)))) (else (display 'ILLEGAL)) ) )
cs480(Prasad) L14Recur 11
Examples
(ops 'x) 0 (ops '(+ y z)) 1 (ops '(if x (+ y z) (+ x z))) 2
cs480(Prasad)
L14Recur
12
Higher-Order Functions
cs480 (Prasad)
L156HOF
13
Equivalent Notations
(define (f x y) (body)) = (define f (lambda (x y) (body) ) )
cs480 (Prasad) L156HOF 14
Func;on
Values
(define tag (lambda (t l) (cons t l)) ) (tag int (1)) -> (int 1)
What characterizes a function? Expression in the body of the definition. The sequence of formal parameters.
cs480 (Prasad)
L156HOF
15
Anonymous
Func;ons
( (lambda (t int l) (cons t l)) (1) )
Name of the function is irrelevant . Simplification: (define tag cons) Assigning function values is similar to assigning primitive values. (first-class values).
cs480 (Prasad) L156HOF 16
Higher-order
Func;ons
In Scheme, function values can be a) passed to other functions. b) returned from functions. c) stored in data structures. FORTRAN, Ada, etc prohibit a), b), c). Pascal allows only a). LISP/C++/Java support approximations in a round about way. LISP : Meta-programming, C++: Function pointers. Java : via Objects (Closures coming in JDK 7) Scala, Python and C# (delegates) are multi-paradigm languages with support for higher-order functions. ML/Scheme are functional languages that treat function values as first-class citizens.
Implementing subprograms using a stack breaks down here. Heap and garbage collection required.
cs480 (Prasad) L156HOF 17
Applica;ons
Abstracting commonly occurring patterns of control. (factoring) Defining generic functions. Instantiating generics. (ORTHOGONALITY)
Eventually contribute to readability, reliability, and reuse. Library: collection of higher-order functions.
cs480 (Prasad) L156HOF 18
Factoring
Commonality
(1 2 n) -> (1 4 n^2)
(define (sql L) (if (null? L) () (cons (* (car L) (car L) ) (sql (cdr L)) )))
cs480 (Prasad)
L156HOF
Built-in map:
(map + '(1 2 3) '(4 5 6))
cs480 (Prasad) L156HOF
= (5 7 9)
20
= =
2 + 4 + 8 + 100 114
= =
8 * 4 * 2 * 100 6400
(a b c)
(b a c)
cs480 (Prasad)
L156HOF
21
Templates/Generics
(define (imax x y) (if (< x y) y x) ) (define (cmax x y) (if (char-<? x y) y x ) )
Generalization
(define (max lt? x y) (if (lt? x y) y x) )
Specialization
(max string-<? a ab )