Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Recursion (computer science) wikipedia , lookup
Design Patterns wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Dirac delta function wikipedia , lookup
Lisp (programming language) wikipedia , lookup
Common Lisp wikipedia , lookup
Functional programming wikipedia , lookup
Function object wikipedia , lookup
Programming Languages Meeting 10 November 8/9, 2016 Planning Ahead • Next week: Short Exam 2 – Program functions – Control structures – Parameter passing – Foundations of functional programming • Definitions • Syntax • Semantic specification 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. Example 3 (6) int A,B; A := 3; // Show Env element B := AV(IV(A)); // Show Env element A := 3; B := AV(IN(A)); // Show Env element A := 3; B := AN(IV(A)); // Show Env element A := 3; B := AN(IN(A)); // Show Env element and Store element and Store element and Store element and Store element and Store element Primitives • • • • • • • first last head tail atom? nil? list? More Primitives • • • • • appendr appendl length The arithmetic functions The comparison functions Functional Forms The four Cs • constant – denoted by overbar – depends on one object • composition – denoted by centered small circle – depends on two functions Functional Forms (2) • construction – denoted by [ ] – depends on n functions – [f]:x <−> <f:x> (n=1) • conditional – denoted using −> and ; – depends on a predicate and two functions Functional Forms (3) • apply-to-all – 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 Exploring reduce • Evaluate /f on the list <1,2,3,4,5,6> when f is o o o o o o + * / ^ max Exploring apply-to-all • Evaluate αf on the list <0, -1, 1, 2, -2> when f is o sgn, the function that returns 1 if its argument is positive, -1 if its argument is negative, and 0 otherwise. o pow2, where pow2(x) = x^2 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 Theorems about Functions • Lengths of lists • Equivalent compositions of functions Programming in LISP Our approach; our rules • Functions may only use our primitives and userdefined functions. • Primitives and functionals may use built-in functions • Functions must not use the LISP equivalents of assignment statements or loops Hints: 1. Think recursively 2. Think in parallel 3. Think with composition 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 Your Turn 1. Create a file using a text editor that contains the LISP definitions of the primitives. Use only the built-in LISP functions listed above. 2. Load your primitives file and test each function thoroughly 3. 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. Your Turn (2) 4. Test your functionals thoroughly. 5. Using the primitives and functionals, define concat, a function that concatenates two lists. 6. 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 (1 2 3 4 5) For next time Complete the Your Turn activities.