1 / 15

Data Abstraction Functional Programming

Data Abstraction Functional Programming. Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it. Interpreter Interaction. > 1 1 > 1.2 1.2 > a error: unbound variable - a if continued: try evaluating symbol again 1> [ back to top level ] >(a b c) error: unbound function - a

orea
Download Presentation

Data Abstraction Functional Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data AbstractionFunctional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

  2. Interpreter Interaction > 1 1 > 1.2 1.2 > a error: unbound variable - a if continued: try evaluating symbol again 1> [ back to top level ] >(a b c) error: unbound function - a if continued: try evaluating symbol again 1> [ back to top level ] > '(a b c) (a b c)

  3. Interpreter Interaction > nil nil > t t > T t > '(a (b c) d) (a (b c) d) > (setq sam 'abc) abc > sam abc

  4. Interaction with the Interpreter > (load "file-name") ; load the content of the file > (compile-file "file-name") ; the compiled version is in file-name.o ; then load in file-name.o > (compile 'func-name) ; compile a particular function > (time (func-name arg1 ... argn)) ; print real and run time for executing ; func-name >(bye) or (quit) or <ctrl>-D ; exit the interpreter

  5. Trace and Untrace > (trace cnt-atom) (cnt-atom) > (cnt-atom '(a b)) Entering: CNT-ATOM, Argument list: ((a b)) Entering: CNT-ATOM, Argument list: (a) Exiting: CNT-ATOM, Value: 1 Entering: CNT-ATOM, Argument list: ((b)) Entering: CNT-ATOM, Argument list: (b) Exiting: CNT-ATOM, Value: 1 Entering: CNT-ATOM, Argument list: (nil) Exiting: CNT-ATOM, Value: 0 Exiting: CNT-ATOM, Value: 1 Exiting: CNT-ATOM, Value: 2 2 > (untrace cnt-atom) nil

  6. Some data abstraction

  7. Sets (defun set_in (el l) (cond ((null l) nil) ((equal el (first l)) t) (t (set_in el (rest l))))) (defun set_insert (el l) (cond ((null l) (cons el l)) ((equal el (first l)) l) (t (cons (first l) (set_insert el (rest l)))))) (defun set_union (l1 l2) (if (null l1) l2 (set_union (rest l1) (set_insert (first l1) l2)))) (defun set_remove (el l) (cond ((null l) l) ((equal el (first l)) (rest l)) (t (cons (first l) (set_remove el (rest l)))))) (defun set_equal (l1 l2) (cond ((null l1) (null l2)) ((set_in (first l1) l2) (set_equal (rest l1) (set_remove (first l1) l2))) (t nil))) (defun set_intersect (l1 l2) (set_intersect-iter l1 l2 nil)) (defun set_intersect-iter (l1 l2 res) (cond ((null l1) res) ((set_in (first l1) l2) (set_intersect-iter (rest l1) l2 (cons (first l1) res))) (t (set_intersect-iter (rest l1) l2 res))))

  8. Association Lists • An association list is a structure that associates a key to a value ((a1 v1) (a2 v2) … (an vn)) (defun al_assoc (key l) (cond ((null l) nil) ((equal key (car (first l))) (cdr (first l))) (t (al_assoc key (rest l))))) (defun al_setval (key val l) (cons (cons key val) l)) (defun al_erase (key l) (cond ((null l) l) ((equal key (car (first l))) (al_erase key (rest l))) (t (cons (first l) (al_erase key (rest l))))))

  9. Exercise: count and sum • Write a function that traverses a tree and prints • the number of symbols in the tree • the sum of the numbers in the tree > (countsum ‘(2 . b)) Sum: 2; Count: 1 > (countsum ‘(2 2 . 3)) Sum: 7; Count: 0 > (countsum ‘((a . b) c)) Sum: 0; Count: 3 > (countsum ‘(1 b 2 c)) Sum: 3; Count: 2

  10. Exercise: hints • To print, use the following construct (format t “control string” arg1 ... argn) > (setq sum 12.4) 12.4 > (setq count 5) 5 > (format t “Sum: ~A; Count: ~A~%” sum count) Sum: 12.4; Count: 5

  11. Exercise: hints • We don’t want two recursions • We want to compute both functions at the same time • We need a way to accumulate two partial results • the partial sum of the numbers seen so far • the number of symbols seen so fa • Pack the result in a cons cell! • car contains sum • cdr contains count

  12. Exercise: count and sum (defun countsum (x) (countsum-iter x (cons 0 0))) (defun countsum-iter (x res) (let ((cnt (car res)) (sum (cdr res))) (cond ((null x) res) ((symbolp x) (cons (+ cnt 1) sum)) ((numberp x) (cons cnt (+ sum x))) (t (countsum-iter (cdr x) (countsum-iter (car x) res))))))

  13. Exercise: count and sum >(trace countsum-iter) >(countsum '(a 1 b (2 . 3) (c . d))) 1> (COUNTSUM-ITER (A 1 B (2 . 3) (C . D)) (0 . 0)) 2> (COUNTSUM-ITER A (0 . 0)) <2 (COUNTSUM-ITER (0 . 1)) 2> (COUNTSUM-ITER (1 B (2 . 3) (C . D)) (0 . 1)) 3> (COUNTSUM-ITER 1 (0 . 1)) <3 (COUNTSUM-ITER (1 . 1)) 3> (COUNTSUM-ITER (B (2 . 3) (C . D)) (1 . 1)) 4> (COUNTSUM-ITER B (1 . 1)) <4 (COUNTSUM-ITER (1 . 2)) 4> (COUNTSUM-ITER ((2 . 3) (C . D)) (1 . 2)) 5> (COUNTSUM-ITER (2 . 3) (1 . 2)) 6> (COUNTSUM-ITER 2 (1 . 2)) <6 (COUNTSUM-ITER (3 . 2)) 6> (COUNTSUM-ITER 3 (3 . 2)) <6 (COUNTSUM-ITER (6 . 2)) <5 (COUNTSUM-ITER (6 . 2)) 5> (COUNTSUM-ITER ((C . D)) (6 . 2)) 6> (COUNTSUM-ITER (C . D) (6 . 2)) 7> (COUNTSUM-ITER C (6 . 2)) <7 (COUNTSUM-ITER (6 . 3)) 7> (COUNTSUM-ITER D (6 . 3)) <7 (COUNTSUM-ITER (6 . 4)) <6 (COUNTSUM-ITER (6 . 4)) 6> (COUNTSUM-ITER NIL (6 . 4)) <6 (COUNTSUM-ITER (6 . 4)) <5 (COUNTSUM-ITER (6 . 4)) <4 (COUNTSUM-ITER (6 . 4)) <3 (COUNTSUM-ITER (6 . 4)) <2 (COUNTSUM-ITER (6 . 4)) <1 (COUNTSUM-ITER (6 . 4)) (6 . 4)

  14. Exercise: count and sum (defun countsum (x) (let* ((res (countsum-iter x (cons 0 0))) (cnt (car res)) (sum (cdr res))) (format t “Symbols Count: ~A; Numbers Sum: ~A~%” cnt sum))) (defun countsum-iter (x res) (let ((cnt (car res)) (sum (cdr res))) (cond ((null x) res) ((symbolp x) (cons (+ cnt 1) sum)) ((numberp x) (cons cnt (+ sum x))) (t (countsum-iter (cdr x) (countsum-iter (car x) res))))))

  15. Homework • Write a program to normalize expressions for lambda calculus • Suggested steps: • Define the ADT for lambda terms • constructors • recognizers • Define substitution primitive • Define recognizer for redex’s • Define beta-reduction rule • Define the normalizer: • recursively traverse the term looking for redex, and reduce • careful with infinite looping!! • Some remarks • homework due by december 20 • homework is closely related to course project!!!

More Related