• date post

23-Dec-2015
• Category

## Documents

• view

214

0

TAGS:

Embed Size (px)

### Transcript of 1 Lisp and Functional Languages Functional forms Referential transparency Function construction...

• Slide 1
• 1 Lisp and Functional Languages Functional forms Referential transparency Function construction Function composition Mapping functions Designing functional languages Other functional language: Scheme, ML, APL,Haskell Applications of Functional Languages Functional and Imperative Language a comparison Reference: Sebesta Chapter 15
• Slide 2
• 2 Functional Programming Language Design The objective of the design of a FPL is to create and use pure functions to the greatest extent possible Process of computation is fundamentally different In an imperative language, operations are executed and the results are stored in variables for later use Management of variables is a constant concern and source of complexity for imperative programming In a purely FPL variables are not necessary as is the case in mathematics Results of each function application are passed as input to other functions or are the final result of all computations
• Slide 3
• 3 Functional Forms Definition: A higher-order function, or functional form, either 1.takes function(s) as parameters 2.or yields a function as its result, 3.or both takes and returns functions Function Parameters (possibly functions) Return Value (possibly a Function)
• Slide 4 L1 ("dogs" " cats" "rats" "gerbils" ) > (setq L2 ; pass function as parameter (sort L1 (function string L2 ("cats" "dogs" "gerbils" "rats")"> (function write-sentence) ; returns defn # > (setq L1 (list "dogs" "cats" "rats" "gerbils")) ("dogs" "cats">
• 4 Viewing Information and Using Functions > (function write-sentence) ; returns defn # > (setq L1 (list "dogs" "cats" "rats" "gerbils")) ("dogs" "cats" "rats" "gerbils") > L1 ("dogs" " cats" "rats" "gerbils" ) > (setq L2 ; pass function as parameter (sort L1 (function string L2 ("cats" "dogs" "gerbils" "rats")
• Slide 5
• 5 Function Construction A functional form that takes a list of functions as parameters, and yields a list of the results of applying each parameter function to a parameter Form: [f, g] means [f, g] (x) (f (x), g (x)) For f (x) x * x * x and g (x) x + 3, [f, g] (4) yields (f (4), g (4)) = (64, 7) Application of multiple functions (passed as parameters) to the same piece of data
• Slide 6
• 6 Function Construction Examples > (defun cube (x) (* x x x)) CUBE > (defun add3 (y) (+ y 3)) ADD3 > (defun myfun (fn1 fn2 data) (list (funcall fn1 data) (funcall fn2 data))) MYFUN > (myfun (function cube) (function add3) 4) (64 7) > (myfun #'cube #'add3 4) ; #' same as (function ) (64 7)
• Slide 7
• 7 Function Composition A functional form that takes two functions as parameters and yields a function containing the first actual parameter function applied to the result of application of the second form Form: f g means f g (x) f ( g (x)) For f (x) x * x * x and g (x) x + 3, f g (x) yields (x + 3) * (x + 3) * (x + 3) Continuous application of multiple functions (passed as parameters). Each function is applied to the result of applying the preceding function(s)
• Slide 8
• 8 Function Composition Examples > (defun cube (x) (* x x x)) CUBE > (defun add3 (y) (+ y 3)) ADD3 > (defun myfun (fn1 fn2 data) (funcall fn1 (funcall fn2 data))) MYFUN > (myfun (function cube) (function add3) 4) 343 > (myfun #'cube #'add3 4) ; #' same as (function ) 343
• Slide 9
• 9 Apply-to-all (Mapping) Functions A functional form that takes a single function as a parameter yields a list of values obtained by applying the given function to each element of a list of parameters Form: means (f, (x 1, , x n )) (f (x 1 ), , f (x n )) For h (x) x * x * x (h, (3, 2, 4)) yields (27, 8, 64) Application of one function to multiple pieces of data
• Slide 10 (mapcar (function mydouble) (list 1 2 3)) (2 4 6) > (m"> (mapcar (function mydouble) (list 1 2 3)) (2 4 6) > (mapcar #'+ (list 1 2 3) (list 10 20 30)) (11 22 33)"> (mapcar (function mydouble) (list 1 2 3)) (2 4 6) > (m" title="10 mapcar Examples > (defun mydouble (num) "returns 2 times its argument" (* 2 num)) MYDOUBLE > (mapcar (function mydouble) (list 1 2 3)) (2 4 6) > (m">
• 10 mapcar Examples > (defun mydouble (num) "returns 2 times its argument" (* 2 num)) MYDOUBLE > (mapcar (function mydouble) (list 1 2 3)) (2 4 6) > (mapcar #'+ (list 1 2 3) (list 10 20 30)) (11 22 33)
• Slide 11
• 11 Lisp The First Functional Language John McCarthy Stanford since 1953 MIT & Dartmouth 1954-1962 Invented Lisp 1958 Pioneered logical and commonsense reasoning Lisps 20th birthday article (from 1980) www2.hawaii.edu/~janst/313/lisp/20th.pdf Web: http://www.formal.stanford.edu/jmc/
• Slide 12
• 12 John McCarthy, professor emeritus of computer science, Stanford Univ., helped to invent the field of artificial intelligence. McCarthy received the Benjamin Franklin Medal in Computer and Cognitive Science on April 24, 2003.
• Slide 13
• 13 Lisp Hardware Problem Limited processing power in 70s and 80s Slow execution of Lisp programs Solution Specialized hardware to improve efficiency of execution and garbage collection Different from von Neumann machine Symbolics Lisp Machine mid 1980s http://www.sts.tu-harburg.de/~r.f.moeller/symbolics-info/symbolics.html Software - OS was written in Lisp! Other lisp machines by LMI (Lisp Machine Inc.) Texas Instruments Explorer
• Slide 14
• 14 Symbolics Lisp Machine Family
• Slide 15
• 15 Optimizations in Lisp Machines Hardware Type Checking Special type bits let a values type be checked efficiently at run-time Hardware Garbage Collection Special "gc" bits make garbage collection efficient Fast Function Calls Efficient Representation of Lists System Software Integrated Programming Environments
• Slide 16
• 16 Scheme and Common Lisp Scheme is a dialect of LISP (mid-1970s) Designed to be a cleaner and simpler version of LISP Uses only static scoping Functions are first-class entities Can be the values of expressions and elements of lists Can be assigned to variables and passed as parameters Scheme has syntax very similar to Lisp We learn Common Lisp most used and practical Sebesta text has Scheme examples
• Slide 17
• 17 More Functional Languages APL A Programming Language Kenneth Iverson at IBM About 1960 (book released in 1962) Not related to ALGOL or other early languages Dynamic typing and dynamic storage allocation Large collection of operators First to include matrix operations Powerful capabilities Poor readability Still in limited use
• Slide 18
• 18 APL Program Example This line of code calculates the prime numbers from 2 to the starting value of R, in this example 20. the "iota function" of R fills a vector (and that will be R again) with numbers from 1 to the value of the variable (20 in this example), the first element is dropped (that is the 1); so to the right of the "/" there will be 2 3 4 5... 18 19 20 the "small.circle-dot-multiply" defines an outer product so all elements of R are multiplied by all elements of R giving a matrix; check whether elements of R are in the matrix and make a vector containing "1"-s at the place where that is true and "0"-s where that is not true inverse that vector and use it to grab the elements from R using the "over" function http://www.thocp.net/software/languages/apl.htm
• Slide 19
• 19 ML (Meta Language) Robert Milner, University of Edinburgh,1980s Designed for program verification A statically scoped functional language Pascal-like syntax Strongly typed, no type coercions Uses type declarations as well as type inferencing to determine the type of a variable Variables are only bound once, they cannot change value after binding
• Slide 20
• 20 ML List functions: hd ( head or car ), tl ( tail or cdr ) The val statement binds a name to a value similar to setq in Lisp Function declaration form: fun ( ) = ; e.g., fun cube (x : int) = x * x * x;
• Slide 21
• 21 Haskell Group formed at 1987 FPL meeting Named after logician Haskell B. Curry Version 1 1990 report centered at the University of Glasgow, Scotland Also Yale, MIT, U of Wellington AU, Cambridge, Chalmers SE, Syntax similar to ML Static scoping Strongly typed, type inferencing Purely functional E.g., no variables, no assignment statements and no side effects of any kind! unlike most other functional languages!
• Slide 22
• 22 Haskell Features Most Important Features Lazy evaluation evaluate no sub-expression until its value is needed "List comprehensions" allow it to deal with infinite lists
• Slide 23
• 23 Haskell Example: Fibonacci Fibonacci numbers illustrates function definitions fib 0 = 1 fib 1 = 1 fib (n + 2) = fib (n + 1) + fib n
• Slide 24
• 24 Haskell Example: Factorial Factorial illustrates guards fact n | n == 0 = 1 | n > 0 = n * fact (n - 1) Special word otherwise can be a guard
• Slide 25
• 25 Applications of Functional Languages APL is used primarily for throw-away programs (why?) LISP is used for artificial intelligence research and applications Knowledge representation Machine learning Natural language processing Modeling of speech and vision AI techniques in Common Lisp power Orbitz Google sponsors summer of code, several Lisp projects Scheme is Used to teach introductory programming (MIT, Stanford) Used in the Final Fantasy film t