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

Slides:



Advertisements
Similar presentations
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Advertisements

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
1 Programming Languages and Paradigms Lisp Programming.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
A problem with functions: arguments must always have values that can be worked out Whenever we call a function we give it arguments. Lisp then works out.
1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing.
Lecturer: Fintan Costello Welcome to Hdip 001 Introduction to Programming.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Directions: 1. Use the Periodic Table to find the Atomic Number (#) and Mass Units for that element. 2. Calculate the number of protons, electrons, and.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types.
Programming in Python Part I Dr. Fatma Cemile Serçe Atılım University
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Working with arrays (we will use an array of double as example)
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Function Design in LISP. Program Files n LISP programs are plain text –DOS extensions vary; use.lsp for this course n (load “filename.lsp”) n Can use.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
A compiler is a computer program that translate written code (source code) into another computer language Associated with high level languages A well.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
You will need Dev C++ to help you with this project. If you do not already have this programming tool on your device you can visit
CSCI1600: Embedded and Real Time Software Lecture 28: Verification I Steven Reiss, Fall 2015.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
CS210: Programming Languages Overview of class Dr. Robert Heckendorn.
Factorization : Difference of Two Squares and Perfect Squares.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
CSE1222: Lecture 1The Ohio State University1. Computing Basics  Computers CPU, Memory & Input/Output (IO)  Program Sequence of instructions for the.
Dayu Zhang 9/10/2014 Lab03. Outline Brief Review of the 4 Steps in Hello.cpp Example Understand endl and \n Understand Comment Programming Exercise -
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Functional Programming Lecture 1 - Introduction Professor Muffy Calder.
Expressions and Assignment
Lecture: MATLAB Chapter 1 Introduction
Conditions and Ifs BIS1523 – Lecture 8.
Functions, Procedures, and Abstraction
CS 36 – Chapter 11 Functional programming Features Practice
Scheme: Basic Functionality
Delayed Evaluation Special forms in Scheme (e.g., if and cond) do not use applicative order evaluation Only one of two or more expressions is actually.
Modern Programming Languages Lecture 18 Fakhar Lodhi
CSC 143 Java Searching.
Functions, Procedures, and Abstraction
Presentation transcript:

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

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

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

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

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

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)

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 “>”

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

‘ ( (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)

>( 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)

> ( 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:

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!

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)