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.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
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.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
1 Programming Languages and Paradigms Lisp Programming.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Fintan Costello.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
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.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Scheme examples. Recursion Iteration is achieved via recursion Selection is achieved via COND Review the following examples to learn to think recursively.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
ISBN Chapter 15 Functional Programming Languages.
Common lisp A functional programming language. Useful URL:
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Control in LISP More on Predicates & Conditionals.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Blocks World Problem. The CS Terminal Specifies the block at the top of the stack. Example CS evaluates to E Note: Evaluates to nil if the stack is empty.
Comparative Programming Languages Functional programming with Lisp/Scheme.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Functional Programming Languages
Sequence, Selection, Iteration The IF Statement
Modern Programming Languages Lecture 20 Fakhar Lodhi
(Functional Programming) Reference: R.Sebesta, Chapter 15
LISP A brief overview.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
Lecture 14: The environment model (cont
Flow of Control.
Abstraction and Repetition
6.001 SICP Interpretation Parts of an interpreter
Modern Programming Languages Lecture 18 Fakhar Lodhi
15.2 Mathematical Functions
Lisp.
Presentation transcript:

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 with those arguments –returns a single answer (evaluates to that answer) Everything within the function is invisible to the user; all they see is the single answer returned.

Iterative (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 Functional programming –Ask the computer to evaluate an expression made up of function calls: >(sqrt (add (square 3) (square 4) )) 5 We want to write a function to do this for any numbers, not just 3 & 4. Getting the hypotenuse of a right-angle triangle

Defining a function in a list (defun hypotenuse (x y) (sqrt (+ (square x) (square y))) ) 1 st element: Special term defun (define function) 2 nd element: name of the function Last element: What function computes 3 rd element: list of function arguments In lisp, the code defining a function is given in a list Given a set of arguments, every function returns (evaluates to) the value of the last element in this defining list

We can tell the interpreter (the listener) to define our function for us, and then we can use it to compute things > (defun hypotenuse (x y) (sqrt (+ (square x) (square y))) ) HYPOTENUSE > (hypotenuse 3 4) 5 What happens when we call (hypotenuse 3 4) like this? > (hypotenuse 3 4) 1.The lisp interpreter looks up “hypotenuse” in the list of functions it knows 2.The interpreter gets the arguments in the function call, and matches them with the argument names in the function definition 4.The interpreter works out the value of this expression (just as if it was typed in directly) and returns the answer 5 (sqrt (+ (square 3) (square 4))) 5 (sqrt (+ (square 3) (square 4))) 3.The interpreter makes a copy of the body of the function, and replaces argument names with arguments values in that copy

The arguments to a function can be: “Raw” data > ( sqrt ( + (square 3) (square 4) ) ) > ( sqrt ( + 9 (square 4) ) ) > ( sqrt ( ) ) > ( sqrt 25 ) 5 >(+ 16 9) A function call This is because a function call always evaluates to (turns into) raw data. Everywhere you could use raw data, you can use a function call that evaluates to that data. 25 >(sqrt 25) 5

In lisp, t means true, nil means false. Logical predicates in lisp >(= 2 2) t >(= 5 3) nil Note that “=“ is a function just like any other. It comes first in the list, it is followed by two arguments, and it evaluates to either t or nil. “=“ is a function for comparing two numbers. We can use a function call wherever we use raw data >(= (hypotenuse 3 4) 5) t

Conditional operators in lisp: if In lisp, an if statement is a list with four elements (defun abs(X) (if (> X 0) X (* -1 X) ) ) 1 st element: Special term if 2 nd element: logical test function Last element: value returned if test is false 3 rd element: value returned if test is true if X > 0 then return X, else return (X by –1) If is a function evaluating either to its then component (3 rd element) or its else component (4 th element)

Cond: a general conditional operator If only allows for a single logical predicate test Cond is more general, allowing a whole sequence of logical predicate tests Lisp tests the predicates in order until one evaluates to t. It then returns the consequent value. (defun stupid(X) (cond ( (= X 1) ‘one ) ( (= X 2) ‘two ) ( (= X 3) ‘three ) ( t ‘huh? ) ) ) Special term cond A clause, consisting of a test and a consequent Note that this test is always true; this is like the “else” part of if

VERY important logical predicates =Two numbers are the same eq Two atoms are the same (eq ‘cat ‘cat) -> t equalTwo lists are the same (equal ‘(cat dog) ‘(cat dog)) -> t nulla list is empty (null ‘()) -> t listpsomething is a list (listp ‘(cat dog)) -> t atompsomething is an atom (atomp ‘cat) -> t