CSC 4101: Programming Languages11Functional ProgrammingChapter 132Functional Programming StyleWrite many small functions (2-liners)Each loop corresponds to 1 functionNo assignment, only function callsWrite base cases of recursion–case for empty list, maybe for atoms–maybe case for singleton listWrite recursive cases–case(s) for nonempty list3Example: Nesting of ParenthesesBase case empty list: 1Base case atoms: 0Recursive case: max (1+car, cdr)Finished function(define (nest x)(cond ((null? x) 1)((not (pair? x)) 0)(else (max (+ 1 (nest (car x)))(nest (cdr x))))))
This preview has intentionally blurred sections.
Sign up to view the full version.