* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download 4.6 Lisp - University of Hawaii
Scala (programming language) wikipedia , lookup
Combinatory logic wikipedia , lookup
Lambda calculus wikipedia , lookup
Anonymous function wikipedia , lookup
C Sharp (programming language) wikipedia , lookup
Lambda lifting wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Lisp (programming language) wikipedia , lookup
Closure (computer programming) wikipedia , lookup
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 1 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 2 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 Parameters (possibly functions) Function Return Value (possibly a Function) 3 Viewing Information and Using Functions > (function write-sentence) ; returns defn #<Interpreted Function WRITE-SENTENCE> > (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<))) ("cats" "dogs" "gerbils" "rats") > L2 ("cats" "dogs" "gerbils" "rats") 4 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 5 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) 6 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) 7 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 8 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, (x1, … , xn)) ≡ (f (x1), … , f (xn)) For h (x) ≡ x * x * x α (h, (3, 2, 4)) yields (27, 8, 64) • Application of one function to multiple pieces of data 9 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) 10 Lisp – The First Functional Language • John McCarthy • Stanford since 1953 – MIT & Dartmouth 1954-1962 • Invented Lisp – 1958 • Pioneered logical and commonsense reasoning • Lisp’s 20th birthday article (from 1980) – www2.hawaii.edu/~janst/313/lisp/20th.pdf • Web: – http://www.formal.stanford.edu/jmc/ 11 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. 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 13 Symbolics Lisp Machine Family 14 Optimizations in Lisp Machines • Hardware Type Checking – Special type bits let a value’s 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 15 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 16 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 17 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 18 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 19 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 <function-name> (<formal-parameters>) = <function-body-expression>; e.g., fun cube (x : int) = x * x * x; 20 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! 21 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 22 Haskell Example: Fibonacci • Fibonacci numbers – illustrates function definitions fib 0 = 1 fib 1 = 1 fib (n + 2) = fib (n + 1) + fib n 23 Haskell Example: Factorial • Factorial – illustrates guards fact n | n == 0 = 1 | n > 0 = n * fact (n - 1) • Special word otherwise can be a guard 24 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 to automate the generation of frames (Square USA) – Used in courses at UH on game programming! 25 Functional vs. Imperative Languages • Imperative Languages: – – – – – Complex syntax Complex semantics Closer to von Neumann hardware – machine language Moderate - efficient execution speed Concurrency is programmer designed • Functional Languages: – – – – – – – Simple syntax Simple semantics Slower execution unless compiled More abstract/higher level – shorter programs, more flexibility Faster development – shorter design & test cycle Programs can automatically be made concurrent More flexible - easily extend the language, creating new ones 26