LISP Data Types Functional Programming Academic Year 2005-2006 Alessandro Cimatti

Slides:



Advertisements
Similar presentations
ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
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.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
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.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
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.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
TES3111 Oct 2001 Data Structures S-Expression - Symbolic expression. It can be an Atom, a List or a collection of S- Expression enclosed by (…) Atom -
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
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.
CSE S. Tanimoto Lisp Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
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.
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.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
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.
Control in LISP More on Predicates & Conditionals.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
From Lambda Calculus to LISP Functional Programming Academic Year Alessandro Cimatti
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
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.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
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,
Section 15.4, 15.6 plus other materials
Lisp S-Expressions: ATOMs
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Lists in Lisp and Scheme
LISP A brief overview.
First Lecture on Introductory Lisp
Modern Programming Languages Lecture 21 Fakhar Lodhi
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Lisp: Representation of Data
Lisp: Using Functions as Data
LISP: Basic Functionality
Modern Programming Languages Lecture 18 Fakhar Lodhi
LISP: Basic Functionality
Common Lisp II.
LISP: Basic Functionality
Lisp.
Lisp: Representation of Data
LISP primitives on sequences
Presentation transcript:

LISP Data Types Functional Programming Academic Year Alessandro Cimatti

LISP: basic data types Everything is a symbolic expression Symbolic expressions can be –Atoms Numbers Symbols –Lists Atom: a sequence of characters beginning with a letter, digit or special character other than “(“ or “)”. –numbers, variable names, etc. –Examples 2, 30, fibo, this-is-an-atom

Recognizers > (atom 'a) T > (atom 12) T >(atom '(12 23)) NIL > (numberp 'a) NIL > (numberp 12) T > (symbolp 'a) T > (symbolp 12) NIL

Basic Math Predicates: –(zerop number ) –(plusp number ) –(oddp integer ) –(evenp integer ) –(integerp atom ) –(floatp atom): returns true if the argument is zero/positive number/etc.; nil, otherwise. Comparison: –(= number more-numbers ) –(/= number more-numbers ) –(< number more-numbers ) –(> number more-numbers ) –(<= number more-numbers ) –(>= number more-numbers ) –(<= )  T; (< )  Nil;

Basic Math Arithmetic Operations: –(+ numbers ) –(- number rest-numbers ) –(* numbers ) –(/ number rest-numbers ) –(1+ number ) –(1- number ) Examples –(1+ 24)  25 –(1- 24)  23 –( )  13 –(+ ) = 0 –(/ 3 4 5)  3/20 –(/ -3) = -1/3 –(* )  1 –(+ (* 2 3) (/ 2 5))  32/5

More Math (max )  5 (min )  -4 (exp number ) = e number (expt base power) = base power –(exp 2)  –(expt 2 3)  2 3 = 8; (log number optional base ) = log base number ; –(log 8 2)  3; (log 3)  1.09; (abs number ) –(abs -5)  5 (signum number ) returns -1, 0 or 1 if the number is negative, zero or positive, respectively (sqrt number); (isqrt number) returns the greater inter less than equal to the exact positive square-root of the number (round number ) (floor number ) (ceiling number ) (truncate number )

Symbolic Expressions Everything is an S-expression (Symbolic- expression). S-expression = ATOM | LIST List: –a collection of S-expression enclosed by parentheses –a sequence of items. –Examples (1 apple ), ((peach (orange)) 3.14 (apricot) apple) –List is treated as function calls first item as the name of a function. –Example ( append ’(a b) ’(c d))  (a b c d)

NIL NIL is a special S-expression It is both an atom and a list. It represents the empty list –can also be written as () It represents falsity –anything else is considered to be true –T is the default value used for true Examples (not nil) = t (not t) = nil (not ‘p) = nil (and ‘p ‘q nil ‘r) = nil (and ‘p ‘q ‘r) = r

The List ADT A list is either –The empty list () or –The list obtained by adding an element to a list The Abstract Data type: –() : -> –cons : x -> –first : -> –rest : ->

Lists: examples The empty list –NIL, also written ( ) One element lists –(a) –(1) –((a)) –(()) Two element lists –(a 1) –(a b) –(a (1))

Operators atom : returns true if the argument is an atom; nil otherwise. –(atom ’(a b))  nil –(atom ’a)  T null : returns true if the argument is nil, nil otherwise. –(null ’(a b))  nil; (null ’a)  nil –(null ’())  T first, rest: –(first ’(a b))  a –(rest ’(a b))  (b) second, third,.., nth: –(second ’(a b))  b –(nth 3 ’(a b c d))  c

On Quoting Lists are treated as function calls When given a list, the interpreter attempts to evaluate the 1 st term as the name of a function The other terms are evaluated using the previously given rules, the results of their evaluations are passed as parameters. The quote operator prevents the interpreter from trying to evaluate an s-expression. > (setq a 5) 5 > (+ 6 (* 3 a)) 21 > (list (quote (+ 2 a)) 1 3) ((+ 2 a) 1 3) > (list ‘(+ 2 a) 1 3) ((+ 2 a) 1 3)

Eval Quote returns an atom or a list preventing its evaluation. > (setq a ‘b) b > (setq b ‘c) c Eval: is the inverse of the quote operator > (+ 3 6) 9 > (eval ’(+ 3 6)) 9 > (eval ’a) b > (eval a) c > (eval ’b) c > (eval b) Error: the variable c is unbound

Operators length : number of elements in a list –(length ’(a b c))  3 cons: append an atom and a list –(cons ’a ’(b c d))  (a b c d) –(cons ’(a) ’(b c d))  ((a) b c d) first: return the first element of a list –(first ’(a b c))  a rest: return the list without the first element –(rest ’(a b c))  (b c) basic properties –(first (cons x y)) = x –(rest (cons x y)) = y

Operators list : creates a list out of atoms and lists –(list ’a ’b ’c)  (a b c) –(list ’a ’((b) c))  (a ((b) c)) –(list ’a ’(1 2) ’((b c) (3 4)))  (a (1 2) ((b c)(3 4))) –(list 1 2 (* 3 4))  (1 2 12) (* 3 4) gets evaluated!

Basics: list append : append two lists > (append ’(a b) ’(c d)) (a b c d) > (append ’(a b) ’(a b) ’(c d)) (a b a b c d) > (append ’(a b) ’() ’(c d)) (a b c d) > (append ’() ’() ’()) ()

Basics: list member : takes two arguments to search for and a list to search for it in, then returns the item if it finds it and everything after it in the list > (member 3 ’((1 2) 3 4 5)) (3 4 5) > (member 2 ’((1 2) 3 4 5) nil

Binding You can bind variable anywhere in a program with the let or let* special forms to create a local context. –let and let*: lexical scope (local context) (let ( ) ) (let ((a 20) b (c 30)) ) –With let*, values from previous variables can be used to define new value. (let* ((a 20) (b (* 2 a)) c) ) –Examples > (let ((a 2)) (+ a 1)) 3 > (let ((a 2) (b 4) (c 0)) (* a b)) 8 > (setq c 3) 3 > (let ((c 5)) c) 5 > c 3

Lexical scope Return value according to the lexical scope where it’s defined. > (setq reg 5) 5 > (defun check-reg () reg) CHECK-REG > (check-reg) 5 > (let ((reg 6)) (check-reg)) 5

Dynamic scope Defvar can be used to define a special variable that is dynamically scoped. > (defvar *sp* 3) *sp* > (defun check-sp () *sp*) CHECK-SP > (check-sp) 3 > (let ((*sp* 4)) (check-sp)) 4 > *sp* 3 > (let ((x 23)) (check-sp)) 3

Conditional Predicates Predicates –Returns either T or Nil. –numberp, listp, symbolp, zerop, etc. –atom: test if an s-expression is an atom. –null :tests for nil. –equal: test if the values are the same. –eq: test if the memory locations are the same. –typep :takes two parameters of an element to test and a type to test it for e.g.) (typep 6 ’number) –Logical operators : and, or, not Nil or the empty list is considered to be false, everything else is true. (and 1 2)  2 (and () 2)  nil

Control structure if statement –(if condition Body1-when-true Body2-when-false) > (if (< 3 5) (+ 2 3) (* 2 3)) 5 cond statement –(cond ((testp1) body1) ((testp2) body2).... (t (default-body)) > (cond ((> 1 2) (+ 2 3)) ((< 2 3) (* 2 3)) (t 10)) 6

Length (defun length (l) (if (null l) 0 (+ 1 (length (rest l))))) length > (length ‘(a b c)) (+ 1 (length ‘(b c))) (+ 1 (+ 1 (length ‘(c)))) (+ 1 (+ 1 (+ 1 (length ‘())))) (+ 1 (+ 1 (+ 1 0))... (+ 1 2) 3

Iterative Length > (defun length (l) (length-iter l 0)) length > (defun length-iter (l cnt) (if (null l) cnt (length-iter (rest l) (+ 1 cnt))) length-iter > (length ‘(a b c)) (length-iter ‘(a b c) 0) (length-iter ‘(b c) 1) (length-iter ‘(c) 2) (length-iter ‘() 3) 3

Append (defun append (l1 l2) (if (null l1) l2 (cons (first l1) (append (rest l1) l2)))))

Running Append > (append ‘(1 2) ‘(3 4 5)) (cons 1 (append ‘(2) ‘(3 4 5))) (cons 1 (cons 2 (append ‘() ‘(3 4 5))) (cons 1 (cons 2 ‘(3 4 5))) (cons 1 ‘( )) ( )

Reverse (defun reverse (l) (if (null l) l (append (reverse (rest l)) (list (first l)))))

Running reverse > (reverse ‘(a b c)) (append (reverse ‘(b c)) ‘(a)) (append (append (reverse ‘(c)) ‘(b)) ‘(a)) (append (append (append () ‘(c)) ‘(b)) ‘(a)) (append (append ‘(c) ‘(b)) ‘(a)) (append (cons ‘c ‘(b)) ‘(a)) (append ‘(c b) ‘(a)) (cons ‘c (append ‘(b) ‘(a))) (cons ‘c (cons ‘b ‘(a))) (cons ‘c ‘(b a)) (c b a) ; Notice something wrong? ; We traverse the lists too many times!!!

Iteration vs Linear Recursion We have seen this happen with numbers The return value of the caller function is the return value of the called function No need to “wait on stack” for called function to return Iteration can be optimized during compilation

Reverse Iterative (defun reverse (l) (reverse-iter l ())) (defun reverse-iter (l acc) (if (null l) acc (reverse-iter (rest l) (cons (first l) acc)))) (reverse-iter ‘(a b c) nil) (reverse-iter ‘(b c) (cons ‘a nil)) (reverse-iter ‘(c) (cons ‘b ‘(a))) (reverse-iter ‘() (cons ‘c ‘(b a))) ‘(c b a)

Append Iterative (defun append (l1 l2) (append-iter l1 l2 ())) (defun append-iter (l1 l2 acc) (if (null l1) (if (null acc) l2 (append-iter l1 (cons (first acc) l2) (rest acc)) (append-iter (rest l1) l2 (cons (first l1) acc))))) > (append-iter ‘(a b) ‘(c d e) ‘()) (append-iter ‘(b) ‘(c d e) ‘(a)) (append-iter ‘() ‘(c d e) ‘(b a)) (append-iter ‘() ‘(b c d e) ‘(a)) (append-iter ‘() ‘(a b c d e) ‘()) (a b c d e)

The Cons ADT A cons is either –The empty list () Or –The list obtained by consing two elements The Abstract Data Type: – : -> –cons : x -> –car : -> –cdr : -> Basic properties –(car (cons x y)) = x –(cdr (cons x y)) = y Compare with lists: the second element of the last cons of a list is always nil > (cons 1 2) (1. 2) > (list 1 2) (1 2) > (cons 1 (cons 2 nil)) (1 2)

Conses cons: takes two s-expressions and returns a list whose car is the 1 st item and whose cdr is the 2 nd item. i.e. insert the 1 st item into the list of 2 nd item. –(cons ’a ’(b c))  (a b c) –(cons ’(a) ’(b c))  ((a) b c) car : returns 1 st element (atom or list) –(car ’(a (b d)))  a –(car ’((a b) c))  (a b) cdr : returns the rest of a list except the 1 st element of a list –(cdr ’(a (b c)))  ((b c)) –(cadr ’((a b) c))  (c) Combinations are possible: cXXXXr where X=(a|r) –(cadr ’(a (b c))) == (car (cdr ’(a (b c))))  (b c)

Dot notation A dot is used to represents a cons with infix notation The printer will try to avoid printing dots explicitly turning them into list notation: > ’(1. 2) (1. 2) > ’(1. nil) (1) > ’(1. (2. 3)) ’(1 2. 3) > ’((1. 2). (3. 4)) ’((1. 2) 3. 4)

Counting the atoms > (defun cnt-atom (l) (cond ((null l) 0) ; nil ((atom l) 1) ; not a cons (t (+ (cnt-atom (car l)) (cnt-atom (cdr l)) ) ) ) ) cnt-atom > (cnt-atom '(a (b c) d (e f (g h)))) 8 > (cnt-atom '(a (b. c) d (e f (g. h)))) 8

Flattening a tree (defun flat (x) (cond ((null x) x) ((atom x) (list x)) (t (append (flat (car x)) (flat (cdr x)))))) f >(flat '( )) ( ) >(flat '(1 (2 3) 4)) ( ) >(flat '(2. 3)) (2 3) >(flat '(() 1 (1 (2 4) (5. 3)))) ( )

Flattening a tree (defun flat1 (x) (cond ((atom x) (list x)) (t (append (flat1 (car x)) (flat1 (cdr x)))))) f >(flat1 '( )) ( NIL) >(flat1 '(1 (2 3) 4)) (1 2 3 NIL 4 NIL) >(flat1 '(2. 3)) (2 3) >(flat1 '(() 1 (1 (2 4) (5. 3)))) (NIL NIL 5 3 NIL)

Flattening a tree (defun flat2 (x) (cond ((null x) x) ((atom x) (list x)) ((null (car x)) (cons nil (flat2 (cdr x)))) (t (append (flat2 (car x)) (flat2 (cdr x)))))) f >(f '( )) ( ) >(f '(1 (2 3) 4)) ( ) >(f '(2. 3)) (2 3) >(f '(() 1 (1 (2 4) (5. 3)))) (NIL )