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.

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
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.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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)
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.
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.
1 COSC3306: Programming Paradigms Lecture 11: Applicative Programming with Lisp Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
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)
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
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.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
Functional Programming 02 Lists
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.
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.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
KU NLP Artificial Intelligence1 Ch 15. An Introduction to LISP q 15.0 Introduction q 15.1 LISP: A Brief Overview  Symbolic Expressions, the Syntactic.
ISBN Chapter 15 Functional Programming Languages.
Common lisp A functional programming language. Useful URL:
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
LISP John McCarthy and Marvin Minsky formed MIT’s AI Project in Suggested reading: John McCarthy’s home page
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
ISBN Chapter 15 Functional Programming Languages.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
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.
Control in LISP More on Predicates & Conditionals.
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)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Introduction to Lisp
Functional Programming: Lisp MacLennan Chapter 10.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
ISBN Chapter 15 Functional Programming Languages.
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,
Functional Programming Languages
Section 15.4, 15.6 plus other materials
Modern Programming Languages Lecture 20 Fakhar Lodhi
Chapter 15 – Functional Programming Languages
LISP A brief overview.
First Lecture on Introductory Lisp
FP Foundations, Scheme In Text: Chapter 14.
CS 36 – Chapter 11 Functional programming Features Practice
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
LISP A brief overview.
Functional Programming: Lisp
Common Lisp II.
Programming Languages
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
Presentation transcript:

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 any identifier of alphanumerics – Think of as a pointer to a property list – Immutable, can only be compared, but also serve as names of variables when used as a variable LISTS are the primary data object Arrays, Structures, Strings (ignore for now) Property Lists – Each atom has a property/value list (think hash table) S-expressions are interpreted list structures

Property Lists – Association Lists Atom a has property p with value v A computing context consists of a set of variables and their current values – ( (key1 val1) (key2 val2)…) – “key” is the name of a variable (a symbol)

Singly linked Lists A “cons” cell has a First field (CAR) and a Rest field (CDR) X (Setf X `(A B C)) () = nil = empty list = “FALSE” – Nil is a symbol, and a list and its value is false. car cdr A B C

Programs Series of function definitions (there are many built-in functions) Series of function calls Read/Eval/Print – (Setf In (Read stdio)) – (Setf Out (Eval In)) – (Print Out) In other words (Loop (Print (Eval (Read))))

List Manipulation Funcs Car, First – (Car (Car (Car L))) Cdr, Rest – (Car (Cdr (Cdr L))) Cons – (Cons ‘1 nil)  (1) – (Cons ‘1 `(2))  (1 2)

List Manipulation Funcs (s) List – (List 1 2 3)  (1 2 3) Quote, ‘ – (Quote (1 2)) = `(1 2) = (1 2) as a list with two elements – Otherwise “1” better be a function! Push, Pop Append Remove Member Length Eval

Arithmetic Incf Decf Plus + Difference – Times * Divide / – 1+2*3=? – (* (+1 2) 3) Remember Infix, Prefix, Polish Postfix?

Functional Composition Cambridge prefix notation (f (g (a (h t)))  f( g( a, h(t)))

Predicates Atom – (Atom `(A)) is false, i.e. nil, because (A) is a list, not an atom – (Atom `A) is true, i.e. 1 (or T) – (Atom A) is either, depending upon its value! A here is regarded as a variable Numberp Null – (Null `(1)) is nil – (Null nil) is T Zerop Eq, Eql, Equal And/Or/Not – (And A B C) = T if the value of all of the variables are non-nil – (Or A B C) = the value of the first one that is non-nil, otherwise nil

Property List Manipulation Putprop/Get/Rempro all defunct in Common Lisp (Get Symbol Property) (Setf (Get Symbol Property) NewValue)

Assignment Atoms are variables if they are used as variables. Syntactic context decides Setq, Set, Rplaca, Rplacd  SETF The general assignment function, does it all (setf (Car L) 1) (setf A 1)

In case you hadn’t noticed PROGRAMS/FUNCTIONS have the same form as DATA Hmmm….

Conditional Expression (If expression expression) or (if expression expression expression) The COND function is like Case, only better! (Cond ( Expression1 *list of expressions1*) ( Expression2 *list of expressions2*) … ( ExpressionN *list of expressionsN*) ) First conditional expression that is true, the corresponding list of expressions is executed, and the value of the last one is returned as the value of the Cond.

Function Defs (Defun Name (variables) *body*) – *body* is a list of S-expressions Equivalent to – (Setf (Get Name func) `(lambda(variables) *body*) Lambda is the primitive (unnamed) function – (Setf X ‘(lambda) (y) (Incr y))) – Now you can pass X to a function where you can evaluate it with Apply, Funcall

The Special LET (Let ( (var1 val) (var2 val) …) *body* ) *body* is a list of expressions

Generators Mapcar Mapc Map (Mapreduce ripped this off from LISP)

Equality Eq – exact same object in memory Eql – exact same object in memory or equivalent numbers Equal – List comparison too, each component should be “equal” to each other – (Equal L M) means every element of L is exactly equal to the corresponding element of M L and M therefore must have the same length and structure, including all sub-components

Examples (Defun Count (n) – (Cond ((Equal n 1) ‘one) ((Equal n 2) ‘two) (T `many))) This function will return one of three Atoms as output, the atom ‘one, or ‘two or ‘many. (Defun Sum (L) (Cond ((Null L) 0) (T (+ (Car L) (Sum (Cdr L))))) This function returns the sum of numbers in the list L. Note: if an element of L is not a number, the “+” function will complain. The LISP debugger will announce it.

More examples (Defun Reverse (L) (Cond ((Null L) nil) (t (Append (Reverse (Cdr L)) (List (Car L) ) ) ) ) This one is not a brain teaser…try it out by hand with a) nil b) a one element list c) a three element list and d) a three element list. See how it works? Recursion and functional programming is very cool.

More examples (Defun Member (x L) (Cond ((Null L) nil) ((Equal x (car L)) L) (t (Member (x (Cdr L) ) ) ) ) Note: if the value of the variable x is actually a member of the list L, the value returned is the “sub-list” where it appears as the “car”. Hmmm… Try it out by hand. Second note: What happens if a) x isn’t a member of L, and b) L isn’t a list?

Let’s do EQUAL Together