Sunteți pe pagina 1din 22

Directory-File

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))

Corresponding constructors and accessors that are automa;cally generated


1. 2. make-file, file-name, file-size, file-content make-dir, dir-name, dir-dirs, dir-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)) ) ) ) )

> (rm-all ' a '(b (b a) a b a)) = (b (b) b) Double recursion


cs480(Prasad) L14Recur 6

rm-all : Structural recursion


Empty list (Basis case) (rm-all 'a '()) First Atom (Recursive case) (rm-all 'a '(a b c a)) (rm-all 'a '(b b c a)) First - Nested list (Recursive case) (rm-all 'a '((a b c) d (a)))
cs480(Prasad) L14Recur 7

Insertion sort (Note: creates a sorted copy)


(define (insert n lon) (cond ((null? lon) (list n)) ((> n (car lon)) (cons (car lon) (insert n (cdr lon)))) (else (cons n lon)) ) ) (define (ins-sort lon) (if (null? lon) '() (insert (car lon) (ins-sort (cdr lon))) ) )

second arg to insert ordered. Postcondition: insert returns an ordered list.


Precondition:
cs480(Prasad) L14Recur 8

Subset (uses OR and AND instead of IF)


(define (subset? ss s) (or (null? ss) (and (member (car ss) s) (subset? (cdr ss) s) ) ) ) 1=> (subset? '(a b c) '(A p 1 C 2 B q r)) #t 2=> (subset? '(a b c) '(p)) #f
cs480(Prasad) L14Recur 9

Expression evaluation : A simple syntax directed translation

expr expr expr

-> x | y | z -> (+ expr expr) -> (if expr expr expr)

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)

(a b c) -> ((a) (b) (c))


(define (brac L) (if (null? L) () (cons (list (car L) ) (brac (cdr L)) )))
19

L156HOF

The map func;on


(define (map fn lis) (if (null? lis) () (cons (fn (car lis)) (map fn (cdr lis)) ) ) )
(map (lambda(x) (* x x)) '(1 2 3)) (map (lambda(x) (list x)) '(a b c))

Built-in map:
(map + '(1 2 3) '(4 5 6))
cs480 (Prasad) L156HOF

= (5 7 9)
20

foldr (reduce) and foldl (accumulate)


(Racket : > Intermediate Level) (foldl * 100 (list 2 4 8))

(foldr + 100 (list 2 4 8))

= =

2 + 4 + 8 + 100 114

= =

8 * 4 * 2 * 100 6400

(foldr cons '(c) '(a b))

(foldl cons ' (c) '(a b))

(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 )

Parameterize wrt comparison


cs480 (Prasad)

Passing function values


L156HOF 22

S-ar putea să vă placă și