Yu-Tzu Lin ( 林育慈 )

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
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.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
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.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
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 COSC3306: Programming Paradigms Lecture 11: Applicative Programming with Lisp Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
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.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
TES3111 October 2001 Artificial Intelligence LISP.
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.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
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 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.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
Functional Programming 02 Lists
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.
COMMON LISP.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
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:
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
04 Control. Control-Blocks Common Lisp has 3 basic operators for creating blocks of code progn block tagbody If ordinary function calls are the leaves.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Comparative Programming Languages Functional programming with Lisp/Scheme.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Functional Programming
Modern Programming Languages Lecture 20 Fakhar Lodhi
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
Getting Started with Lisp
LISP A brief overview.
First Lecture on Introductory Lisp
J.E. Spragg Mitthögskolan 1997
FP Foundations, Scheme In Text: Chapter 14.
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
Lisp: Representation of Data
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Lisp.
Lisp: Representation of Data
The general format of case is the following: (case <key form>
Presentation transcript:

Yu-Tzu Lin ( 林育慈 )

Information 2

Information (cont.) References ANSI Common LISP, by Paul Graham, Prentice Hall, (A good introductory book) LISP, by Patrick Henry Winston and Bertbold Klaus Paul Horn, Addison Wesley, (A quick guide to write Lisp) Introduction to functional programming, by Richard Bird and Philip Wadler, Prentice-Hall, Functional programming, by Jeroen Fokker, (Free for educational purposes) 3

Information (cont.) Grade Homework: 30% Midterm exam: 30% Final project: 40% "I can do everything through him who gives me strength“ Phillipians 4:13 4

Syllabus Introduction Lists Specialized Data Structures Control Functions Input and output Symbols Numbers Macros Structure 5

Introduction-Functional Programming Computers are expensive in early days → make the programming language resemble the computer architecture → imperative programming e.g. Pascal, C, … Statement 1; Statement 2; Statement 3; … Statement n; 6

Introduction-Functional Programming There were already methods to solve problems before the invention of computers Functions play a much more central roles Function Expresses the connection between parameters (input) and the result (output) Is a good way of specifying a computation 7 InputOutput Function Result=f(parameter 1, parameter 2, …, parameter p)

Introduction-Functional Programming A functional program consists of the definition of one or more functions 8 Function 1 Function 2 Function 3 … Function n

Introduction-Lisp Lisp was first conceived by John McCarthy in 1956 The first functional programming language Not the language satisfies all modern demands Other functional programming languages came to existence for different purposes ML Scheme (an adjustment to Lisp) Haskell … Designed to evolve You can use Lisp to define new lisp operators 9

Introduction-Lisp Example 10 ; Lisp /* C */ (defun sum (n) int sum(int n){ (let ((s 0)) int i, s = 0; (dotimes (i n s) for (i = 0; i < n; i++) (incf s i ) ) ) ) s += i; return(s); }

Introduction-Lisp Start to write CLISP 中文用法 e.html e.html 11

Introduction-Lisp An interactive front-end (toplevel) > is the toplevel prompt > 1 (you type 1) 1 (the system print its value  evaluate to itself) > (another prompt to say that it’s ready for more) > (+ 2 3) 5 + is the operator The numbers 2 and 3 are arguments Prefix notation ? 12

Introduction-Lisp > (+) 0 > (+ 2) 2 > (+ 2 3) 5 > ( ) 14 13

Introduction-Atoms and Lists Expressions can be nested > (/ (-7 1) (- 4 2)) ? Lisp uses a single notation to express all ideas Atom List 14

Introduction-Evaluation (+ 2 3) + is a function 2 and 3 are arguments (+ 2 3) is a function call Function evaluation 1: the arguments are evaluated, from left to right 2: the values of the arguments are passed to the function named by the operator 15

Introduction-Evaluation (/ (- 7 1) (- 4 2)) 1: evaluate (- 7 1) 1.1: evaluate 7 1.2: evaluate 1 1.3: pass 7 and 1 to the function -, which returns 6 2: evaluate (- 4 2) 2.1: evaluate 4 2.2: evaluate 2 2.3: pass 4 and 2 to the function -, which returns 2 3: pass 6 and 2 to the function /, which returns 3 16

Introduction-Evaluation > (/ 1 0) Error: Division by zero. > (quote (+ 3 5)) (+ 3 5) > ‘(+ 3 5) (+ 3 5) Quote takes a single argument and just returns it verbatim A way to protect expressions from evaluation 17

Introduction-Data Lisp offers all the data types we find in most other languages Integer e.g. 256 String e.g “ora et labora” … Special data type Symbol Word List Is represented zero or more elements enclosed in parentheses 18

Introduction-Data Symbol Does not evaluate to itself If you want to refer to a symbol, you should quote it > ‘Artichoke ARTICHOKE List The elements can be of any type, including lists You have to quote lists, or lisp would take them for functions calls > ‘(my 3 “Sons”) (MY 3 “Sons”) > ‘(the list (a b c) has 3 elements) (THE LIST (a b c) HAS 3 ELEMENTS) One quote protects a whole expression 19

Introduction-Data Build lists by calling list > (list ‘my (+ 2 1) “Sons”) (MY 3 “Sons”) The list is evaluated to return The list itself if it is quoted Its value if it is not quoted > (list ‘(+ 2 1) (+ 2 1)) ((+ 2 1) 3) Lisp programs are expressed as lists You can write programs to generate programs 20

Introduction-Data Empty list > ( ) NIL nil NIL You don’t have to quote nil because nil evaluates to itself 21

Introduction-List Operators Cons (cons ) > (cons ‘a ‘(b c d)) (A B C D) > (cons ‘a (cons ‘b nil)) (A B) > (list ‘a ‘b) (A B) 22

Introduction-List Operators First > (first ‘(fast computers are nice)) FAST Rest > (rest ‘(fast computers are nice)) (COMPUTERS ARE NICE) Car > (car ‘(a b c)) A > (cdr ‘(a b c)) (B C) > (car (cdr (cdr ‘(a b c d)))) C Cxxxxr x is a or d 23

Introduction-Truth The symbol t is the default representation for truth Like nil, t evaluates to itself (listp object) Returns true when object is a list > (listp ‘(a b c)) T > (listp 27) NIL A function whose return value is intended to be interpreted as truth or falsity is called a predicate Common Lisp predicates often have names that end with p 24

Introduction-Truth (null object) Returns true when object is nil > (null nil) T (not object) Returns true when object is nil > (not nil) T 25

Introduction-Truth > (if (listp ‘(a b c)) (+ 1 2) (+ 5 6)) 3 > (if (listp 27) (+ 1 2) (+ 5 6)) 11 > (if (listp 27) (+ 2 3)) NIL > (if ) 1 Everything except nil also counts as true 26

Introduction-Truth (and expression) Evaluates the expression in order, returning nil immediately if one evaluates to nil, or if they all return true, the value of the last. Returns t if given no arguments. > (and t (+ 1 2)) 3 27

Introduction Exercises Describe what happens when the following expressions are evaluated (+ (- 5 1) (+ 3 7)) (list 1 (+ 2 3)) (if (listp 1) (+ 1 2) (+ 3 4)) (list (and (listp 3) t) (+ 1 2)) 28

Introduction-Functions defun > (defun our-fun (x) (car (cdr (cdr x)))) OUR-FUN > (our-fun ‘(a b c d)) C A symbol has to be quoted because otherwise it will be treated as a variable > (> (+ 1 4) 3) T (defun sum-greater (x y z) (> (+ x y) z)) SUM-GREATER > (sum-greater 1 4 3) T 29

Introduction-Functions Lisp makes no distinction between a program, a procedure, and a function Functions do for everything 30

Introduction-Recursion (defun our-member (obj lst) (if (null lst) nil (if (eql (car lst) obj) lst (our-member obj (cdr lst))))) > (our-member ‘b ‘(a b c)) (B C) > (our-member ‘z ‘(a b c)) NIL 31

Introduction-Input and Output > (format t “~A plus ~A equals ~A. ~%” 2 3 (+ 2 3 )) 2 plus 3 equals 5. NIL The first line is displayed by format The second line is the value returned by the call to format t indicates that the output is to be sent to standard output The second argument is a string that serves as a template for output ~% indicates a newline 32

Introduction-Input and Output Read No arguments Reads from the standard input (toplevel) (defun ask (string) (format t “~A” string) (read)) > (ask “How old are you?”) How old are you?

Introduction-Variables Let One of the most frequently used operators in Common Lisp > (let ((x 1) (y 2)) (+ x y)) 3 (defun ask-number ( ) (format t “Please enter a number. “) (let ((val (read))) (if (numberp val) val (ask-number)))) 34

Introduction-Variables Global variable > (defparameter *x* 32) *x* > *x* 32 (defconstant limit (+ *x* 1)) (boundp symbol) Returns true iff symbol is the name of a special variable > (boundp ‘*x*) T 35

Introduction Exercises What does this function do? (defun enigma (x) (and (not (null x)) (or (null (car x)) (enigma (cdr x))))) 36

Introduction-Assignment > (setf *x* 98) 98 > (let ((n 10) (setf n 2) n) 2 > (setf x (list ‘a ‘b ‘c)) (A B C) > (setf (car x ) ‘n) N > x (N B C) 37

Introduction-Assignment (setf a b c d e f) (setf a b) (setf c d) (setf e f) 38

Introduction-Functional programming in Lisp Functional programming means writing programs that work by returning values, instead of by modifying things > (setf lst ‘(c a r a t)) (C A R A T) > (remove ‘a lst) (C R T) > lst (C A R A T) (setf new_x (remove ‘a x)) 39

Introduction-Iteration (defun show-squares (start end) (do ((i start (+ i 1))) ((> i end) ‘done) (format t “~A ~A~%” i (* i i)))) > (show-squares 2 5) DONE 40

Introduction-Functions as Objects In Lisp, functions are regular objects, like symbols or strings or lists (function function_name) Returns the function whose name is function_name > (function +) # > #’ + > (apply #’+ ‘(1 2 3)) 6 > ( ) 6 41

Introduction Exercises Mary is trying to write a function that returns the sum of all the non-nil elements in a list. Explain why this program cannot give a correct answer. (defun summit (lst) (remove nil lst) (apply #’+ lst)) 42