This preview shows pages 1–3. Sign up to view the full content.
CSC 4101: Programming Languages
1
1
Functional Programming
Chapter 13
2
Functional Programming Style
Write many small functions (2liners)
Each loop corresponds to 1 function
No assignment, only function calls
Write base cases of recursion
–
case for empty list, maybe for atoms
–
maybe case for singleton list
Write recursive cases
–
case(s) for nonempty list
3
Example: Nesting of Parentheses
Base case empty list: 1
Base case atoms: 0
Recursive 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.
View Full Document CSC 4101: Programming Languages
2
4
Example: Integer Equation
Given: lengths l1, l2, l3, len
Can len be constructed from pieces
of lengths l1, l2, and l3?
(define (test len l1 l2 l3)
(if (<= len 0) (= len 0)
(or (test ( len l1) l1 l2 l3)
(test ( len l2) l1 l2 l3)
(test ( len l3) l1 l2 l3))))
5
ML vs. Scheme
Scheme
–
primitive syntax
–
dynamically typed
–
Lists as builtin data type
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 12/31/2011 for the course CSC 4101 taught by Professor Kraft,d during the Fall '08 term at LSU.
 Fall '08
 Kraft,D
 Functional Programming, Recursion

Click to edit the document details