Download presentation
Presentation is loading. Please wait.
1
Programming Languages
Meeting 10 November 8/9, 2016
2
Planning Ahead Next week: Short Exam 2 Program functions
Control structures Parameter passing Foundations of functional programming Definitions Syntax Semantic specification
3
Reflections on Homework
Program function, II.4, variables in range 0..maxint What is the value of x when a while loop that has condition x<>b terminates? If sum(1,25,1,k,A[k]) sums the first 25 elements of the array A, by pattern matching sum( _ , _ , _ , _ , ___ ) sums the first 7 powers of 3.
4
Example 3 (6) int A,B; A := 3; // Show Env element and Store element B := AV(IV(A)); B := AV(IN(A)); B := AN(IV(A)); B := AN(IN(A));
5
Primitives first last head tail atom? nil? list?
6
More Primitives appendr appendl length The arithmetic functions
The comparison functions
7
Functional Forms The four Cs constant composition denoted by overbar
depends on one object composition denoted by centered small circle depends on two functions
8
Functional Forms (2) construction conditional denoted by [ ]
depends on n functions [f]:x <−> <f:x> (n=1) conditional denoted using −> and ; depends on a predicate and two functions
9
Functional Forms (3) apply-to-all insert (also known as reduce)
denoted by αf depends on one function works only on lists insert (also known as reduce) denoted by /f depends on one function with a number of properties
10
Exploring reduce Evaluate /f on the list <1,2,3,4,5,6> when f is
+ - * / ^ max
11
Exploring apply-to-all
Evaluate αf on the list <0, -1, 1, 2, -2> when f is sgn, the function that returns 1 if its argument is positive, -1 if its argument is negative, and 0 otherwise. pow2, where pow2(x) = x^2
12
Enhanced apply-to-all
αf applies f to each element of a list Called apply-to-all apply-to-all1 applies a dyadic function (two arguments) to a fixed object and a list of objects apply-to-all2 applies a dyadic function to a pair of equal length lists
13
Theorems about Functions
Lengths of lists Equivalent compositions of functions
14
Programming in LISP Our approach; our rules
Functions may only use our primitives and user-defined functions. Primitives and functionals may use built-in functions Functions must not use the LISP equivalents of assignment statements or loops Hints: Think recursively Think in parallel Think with composition
15
Built-In Lisp Functions
car cdr reverse length cons append defun cond error funcall mapcar and, or, not fixp numberp zerop, plusp, minusp equal atom null mod +, - , *, / times
16
Your Turn Create a file using a text editor that contains the LISP definitions of the primitives. Use only the built-in LISP functions listed above. Load your primitives file and test each function thoroughly Create a second file using a text editor that contains the LISP definitions of the functionals. Note: You do not have define composition or condition; they are built into the LISP system.
17
Your Turn (2) Test your functionals thoroughly.
Using the primitives and functionals, define concat, a function that concatenates two lists. Using the primitives and functionals, define IG, the index generator function. (IG n) creates a list of length n containing the first n positive integers. E.g. (IG 5) returns ( )
18
For next time Complete the Your Turn activities.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.