Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Writing functions in OCaml. Defining a simple function # let add (x, y) = x + y;; val add : int * int -> int = Notice what this says: –add is a value.
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.
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.
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.
LISP Programming. LISP – simple and powerful mid-1950’s by John McCarthy at M.I.T. “ LIS t P rocessing language” Artificial Intelligence programs LISP.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
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.
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
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.
Python November 14, Unit 7. Python Hello world, in class.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
TES3111 October 2001 Artificial Intelligence LISP.
JavaScript, Third Edition
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.
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.
Digital Electronics Data Structures LISP
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.
PRACTICAL COMMON LISP Peter Seibel 1.
PRACTICAL COMMON LISP Peter Seibel 1.
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.
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.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Unit 3: Java Data Types Math class and String class.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Iteration Chapters 6 & 7. Iteration in LISP n LISP (unlike Prolog) allows iteration –mapcar, remove-if(-not), count-if, find-if for special purpose iteration.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Built-in Data Structures in Python An Introduction.
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.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
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.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
You can access the members of a list with the functions car (or first) and cdr (or rest): (setf list '(a b c)) (car list) ⇒ a (first list) ⇒ a (cdr list)
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Scheme (Section 11.2) CSCI 431 Programming Languages Fall 2003.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Section 15.4, 15.6 plus other materials
CS 326 Programming Languages, Concepts and Implementation
Variables, Expressions, and IO
Lists in Lisp and Scheme
LISP LISt Processing.
CS 36 – Chapter 11 Functional programming Features Practice
Defining Macros in Lisp
LISP LISt Processing.
Abstraction and Repetition
LISP LISt Processing.
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Lisp.
Defining Macros in Scheme
LISP primitives on sequences
Lists in Lisp and Scheme
Introduction to Computer Science
Presentation transcript:

Introduction to LISP Atoms, Lists Math

LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical functions –mapping from one domain to another

LISP Basics n Common objects are atoms and lists –one thing is both n Atoms as in Prolog – a thing in itself n Lists as in Prolog – a sequence of things n Syntax is different, tho’

Interacting with LISP n You write something down n LISP tells you its value –it’s an error if it has no value n What you write down is an atom or a list n What you get back is an atom or a list

Evaluating Atoms n When you give something to LISP, it evaluates it n Some things evaluate to “themselves” –numbers, for example > n Most things do not – more later

Math in LISP n LISP evaluates mathematical expressions –math functions use their common symbols n LISP uses prefix notation –operator comes before the operands n Math expression in parentheses > ( ) 33

Math Expressions as Lists n Lists set off in parentheses –no commas between elements n Operator is first element of the list n Operands come after –can actually have multiple operands > ( ) 70

Math Expressions as Operands n Operands can be math expressions > (+ 23 (* 2 5)) 33 n Expression is evaluated & result used in place of the expression –multiply 2 by 5 to get 10 –add 23 and 10 to get 33

Exercise n Evaluate these expressions: 57 ( ) (* ) (+ (* 2 5) (/ 6 3) (– 6 1)) (+ ( ) (* 2 5 7) (* 2 (+ 2 1))) (/ )

Function Calls n Function calls are lists –function name first element of list –arguments come after –whole in (parentheses) – not [brackets] –no commas – elements separated by spaces n Just like with math –function goes inside the parentheses

Function Calls n Some functions are built in… > (sin 3.14) > (max –9 87 5) 88 n …and you can create your own (more later) > (factorial 4) 24

Very Common Mistake n Function name goes inside parentheses –always always always (except…) –even when it’s a name –even when inside other parentheses > (+ max(3 10) min(10 8)) Error: …MAX… (also min) > (+ (4 * 5) (7 – 3)) Error: …4… (also 7)

Case Sensitivity n LISP has none –it’s an old language – late 1950s n Everything gets mapped to upper case –well, almost everything n Doesn’t matter which way you write things –max, MAX, Max, and mAx all the same: MAX –no atom/variable distinction

Atoms/Variables n Atoms can have values > pi d0 > (sIn Pi) d-16 n LISP variables like imperative variables –value can be changed –except some declared constant

Evaluating Atoms n Normal atoms start with no value –asking for their value is an error > mark Error: … unbound variable `MARK'. n To get thing itself back, put a single quote in front of it > ‘mark MARK Note: one single quote at the front; no “closing” quotation mark

Assigning Values to Atoms n Various set functions > (set ‘comp2043 ‘mark) MARK > comp2043 MARK > (setf comp2043 ‘mark) MARK Text uses SETF instead of SET same but first argument not quoted known as a “special form” Set returns the value it assigned just like = in c

Using Atoms with Values n Atom just gets replaced with its value –unless you quote it, of course > comp2043> ‘comp2043 MARKCOMP2043 > (set ‘comp2113 comp2043) MARK > comp2113 MARK Unquoted atom (comp2043) is replaced by its value (mark)

Exercise: What Values? > (set ‘x 10) > x > ‘x > (set ‘y 50) > (+ x y) > (set ‘v ‘x) > (set v 20) > (+ x y)

Argument Evaluation > comp2043 MARK > (set comp2043 ‘newval) NEWVAL > comp2043 MARK > mark NEWVAL n All arguments get evaluated –except special forms n Quotation mark inhibits evaluation –‘ is a special form

Set and SetF n Set is actually deprecated –in danger of going away n Use setf instead (short for “set field”) n Setf is a special form –means some arguments not evaluated –lots of these in LISP – gotta just remember > (setf mark ‘newval)  (set ‘mark ‘newval) first argument of setf not evaluated

More Atoms n Atoms in LISP are pretty much any string –some punctuation OK! –no spaces, commas, semi-colons, colons > ‘all-that&a-bag-of-chips! ALL-THAT&A-BAG-OF-CHIPS! > ‘23a+b 23A+B

Values of Values n Taking a value only goes one step –value of x is Y, value of y is Z  value of x is Y > (setf x ‘y) Y > (setf y ‘z) Z > x Y It doesn’t say Z here because x’s value isn’t Z

Lists as Data n Lists can be arguments to functions > (setf mark ‘(comp2043 comp2113)) (COMP2043 COMP2113) n Comp2043 and comp2113 are not evaluated –part of a quoted list n Value of atom MARK is a list of two atoms –each of those atoms has a value –but those values are not MARK’s values

Quoted Lists n Single quote mark applies to the whole list –not treated as a function at all –even if it looks like a function > ‘(setf x y) (SETF X Y) Note: quote is in front of the list the list is not evaluated nor any of its “arguments”

The Empty List n The empty list has no elements –parentheses with nothing in them (spaces OK) n Evaluates to NIL –nil is just another name for the empty list > ( ) NIL n Can quote or not – doesn’t matter > ‘( ) NIL

Exercises n What values do the following assign? (setf shortlist ‘( )) shortlist = ? (setf sum1 ( )) sum1 = ? (setf sum2 ‘( )) sum2 = ?

Working with Lists n Lists are a big part of LISP n Need to be able to: –take lists apart –put lists together n Functions for doing just that

Taking Lists Apart n Primary functions for splitting lists: –first returns first element of a list –rest returns everything but first element of list > (first ‘(a b c d)) A > (rest ‘(a b c d)) (B C D)

Removing from Lists n First & rest do not change a variable’s value > (setf old ‘(b c d)) (B C D) > (first old) B > (rest old) (C D) > old (B C D) old’s value remains same

Second, Third & Last n Common LISP provides second, third, …, tenth as built-in functions > (third ‘(a b c d)) C n Last gives list with just last element in it > (last ‘(a b c d)) (D)

Lists in Lists n List may have another list as an element > ‘((a b) (c d) (e f)) ((A B) (C D) (E F)) n First, second, &c return top-level elements > (first ‘((a b) (c d) (e f))) (A B) > (rest ‘((a b) (c d) (e f))) ((C D) (E F))

Elements That Aren’t There n Ask for tenth element of list with only four –not an error – returns nil instead > (tenth ‘(a b c d)) NIL n First & rest similarly > (rest ()) NIL

CAR and CDR n CAR & CDR are same as first & rest –old-fashioned function names n CADR is the CAR of the CDR –the first of the rest = the second –similarly CADDR = third n CDDR = CDR of CDR = all but 1 st & 2 nd n Can combine in weird ways, too

Exercise n Evaluate the following: (first ‘( )) (rest ‘( )) (first (rest ‘( ))) (third ‘( )) (first ‘(rest ( ))) (cddar ‘((1 2 3) (4 5 6) (7 8 9)))

Putting Lists Together n Append combines two lists into one –works for more than two, too > (append ‘(a b c) ‘(d e f)) (A B C D E F) > (append ‘(1 2 3) ‘(a b c) ‘(do re mi)) (1 2 3 A B C DO RE MI) > (append () () () () () ‘(a) () () () () ‘(b)) (A B)

Putting Lists Together n List makes a list out of its arguments –as many as you give it > (list ‘a ‘b ‘c ‘(d e f) ‘g) (A B C (D E F) G) > (list (* 1 1) (* 2 2) (* 3 3) (* 4 4)) ( )

Adding to the Front of a List n Cons puts an element on the front of a list > (cons ‘a ‘(b c)) (A B C) > (setf old ‘(b c d)) (B C D) > (cons ‘a old) (A B C D) > old (B C D) Note: cons returns a new list old value not changed

Making Lists n Make multiple lists into one: –append n Make a list out of various things –list n Put one element at front of a list –cons n None of them change variable values!

Exercise n What function is used to: –make ( ) from (1 2 3) and (4 5)? –make ( ) from 1 and ( )? –make ((1 2 3) (4 5)) from (1 2 3) and (4 5)? –make ((1 2 3) 4 5) from (1 2 3) and (4 5)? –make (1 2 3 (4 5)) from 1, 2, 3 and (4 5)?

Adding to the Front of a List (II) n Use push and pop to modify a variable > (setf old ‘(b c d)) (B C D) > (push ‘a old) (A B C D) > old (A B C D) > (pop old) A > old (B C D)

Length of a List n Length returns the length of a list > (length ‘(a b c d e)) 5 n Returns top-level length > (length ‘((a b) (c d) (e f (g) h i))) 3

Exercise n Evaluate the following (list (append ‘(1 2) ‘(3 4)) (cons ‘a ‘(b c d)) (cons ‘a ‘(b c d)) (length ‘(a s d f)) (length ‘(a s d f)) (rest (append ‘(4 5 6) (cons 1 ‘(2 3))))) (rest (append ‘(4 5 6) (cons 1 ‘(2 3)))))

Next Time n Defining functions in LISP –Chapters 3 & 4