Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Fintan Costello."— Presentation transcript:

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

2 Course overview Part 1 Around 6 weeks The idea of functional programming: functions & recursion in Lisp Weekly labs: Lisp programing tasks & assessments each week Part 2 Around 2 weeks Common Lisp: other aspects Part 3 Around 4 weeks Using Common Lisp: a case study on AI search methods Case-study work

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) Think of it like this: after the function call has been evaluated, the returned value replaces the function call 3 (the square root of 9 is 3)

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)Set field in the variable x to have a value of 9 > xAsk lisp the value of x > (sqrt x)Call square root func > xAsk the value of x again 9 9 3

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

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

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

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: (follow the link for functional programming)


Download ppt "Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Fintan Costello."

Similar presentations


Ads by Google