1 / 13

Functional Programming COMP2003

Functional Programming COMP2003. A course on functional programming using Common Lisp Dr Fintan Costello. Course overview. What you will gain from this course. After finishing this course, you will be able to program in Common Lisp (an important language, especially for next years AI course!)

crevan
Télécharger la présentation

Functional Programming COMP2003

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Fintan Costello

  2. Course overview

  3. What you will gain from this course • After finishing this course, you will be able to program in Common Lisp (an important language, especially for next years AI course!) • Via Common Lisp, you will have a strong understanding of the two core functional programming ideas: • functions and recursion

  4. Comparing two ways of programming • Imperative programming • tell the computer to execute a series of actions in a given order: • int A= 3 * 3; • int B= 4 * 4; • int C= A + B; • int Ans= sqrt (C); • println(Ans); • 5 • You plan the steps the computer takes • Functional programming • Ask the computer to evaluate an expression made up of function calls: • >(sqrt (add (square 3) (square 4) ) ) • 5 • Computer works out the value of the expression • Computer figures out the steps it takes

  5. Why functional programming? • A different approach to programming • Used in numerical processing (Mathematicia), natural-language processing, and AI research • Very concise; used for rapid-prototyping • Functional programming will add to your programming ability in all languages

  6. Functions • A function is something which • takes some arguments as input • does some computing with those arguments • returns a single answer (evaluates to that answer) >(sqrt 9) 3 (the square root of 9 is 3) Think of it like this: after the function call has been evaluated, the returned value replaces the function call

  7. Using Lisp: asking the interpreter • In Java, you write a program, compile, and run it • In lisp, you ask the interpreter (also called the listener) to evaluate functions • Interactive; like asking questions and getting answers • Every question starts with a function name, followed by the function’s arguments • You can write your own functions; this is what programming in lisp is all about. • In lectures we represent the interpreter (listener) as “>”

  8. Another Functions example • When you call a function in Lisp, that function usually does not change the values of the arguments it has been given. >(setf x 9) Setfield in the variable x to have a value of 9 > x Ask lisp the value of x 9 > (sqrt x) Call square root func 3 > x Ask the value of x again 9

  9. A list of data is Quoted Lisp stands for LISt Processing In lisp, every list Starts with an open bracket Contains elements (these single elements are called atoms) Contains elements (elements of a list can be lists themselves) Ends with a closing bracket ‘(aardvark antelope ape apple) ‘ ( (aardvark antelope ape apple) (bear banana) (camel cat carrot) 12 cranberry )

  10. > ( first ‘((aardvark antelope ape apple) hello (banana) (camel cat carrot) 12 cranberry) ) >( first ‘(aardvark antelope ape apple) ) >( rest ‘(aardvark antelope ape apple) ) Function calls are lists too In lisp, a function call is a special type of list Ends with a closing bracket Starts with an open bracket First element is the name of the function Other elements are args (can be lists or lists of lists) (antelope ape apple) aardvark (aardvark antelope ape apple) rest is a function that takes a list and returns the rest of the list (leaving out the first element) First is a function that takes a list and returns the 1st element Function call lists are not quoted: they are actions, not data!!

  11. Function arguments can be calls to other functions Every function call Starts with an open bracket First element is the name of the function Other elements are args (can be function calls) Ends with a closing bracket > ( sqrt ( + (square 3) (square 4) ) ) Evaluation: > ( sqrt ( + (square 3) (square 4) ) ) > ( sqrt ( + 9 (square 4) ) ) > ( sqrt ( + 9 16 ) ) > ( sqrt 25 ) 5

  12. Important!!! Brackets have to match (sqrt (+ (square 3) (square 4) ) ) is correct (sqrt (+ (square 3) (square 4) ) is wrong (sqrt (+ (square 3 (square 4) ) ) is wrong (sqrt (+ (square 3)) (square 4) ) ) is wrong and so on. If brackets don’t match, lisp won’t understand an expression (it wont make any sense) Always check the brackets!

  13. Lab exercises (starting next week) • Start Allegro common lisp; • Various lines for you to try out • Write a function to get the hypotenuse of a triangle • Save that function to a file These powerpoint slides, and practicals for this course, are available at: http://www.cs.ucd.ie/staff/fcostello//home/default.htm (follow the link for functional programming)

More Related