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.

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.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
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.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
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 Functional programming Languages And a brief introduction to Lisp and Scheme.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
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.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
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.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
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 -
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
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.
Programming Languages I LISP
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
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. 
Digital Electronics Data Structures LISP
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
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,
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
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.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
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
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 28 / 2006 Instructor: Michael Eckmann.
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.
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.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Functional Programming
Functional Programming Languages
Functional Programming
Lisp S-Expressions: ATOMs
CS 326 Programming Languages, Concepts and Implementation
LISP LISt Processing.
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
FP Foundations, Scheme In Text: Chapter 14.
Lisp: Representation of Data
LISP LISt Processing.
LISP: Basic Functionality
LISP LISt Processing.
Modern Programming Languages Lecture 18 Fakhar Lodhi
LISP: Basic Functionality
topics interpreters meta-linguistic abstraction eval and apply
Common Lisp II.
LISP: Basic Functionality
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lisp.
Lisp: Representation of Data
LISP primitives on sequences
Presentation transcript:

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 presents a very different way to think about programming from the “algorithmic” languages.

The basis of LISP is a list One constructs a list by enumerating elements inside a pair of parentheses. For example, here is a list with four elements (the second element is also a list): (23 (this is easy) hello 821) The elements in the list, which are not lists, are called “atoms.” For example, the atoms in the list above are: 23, this, hello, 821, easy, and is. Everything in LISP is either an atom or a list (but not both). The only exception is “NIL,” which is both an atom and a list. It can also be written as “()” – a pair of parentheses with nothing inside.

All LISP statements are function calls All statements in LISP are function calls with the following syntax: (function arg 1 arg 2 arg 3 … arg n ). To evaluate a LISP statement, each of the arguments (possibly functions themselves) are evaluated, and then the function is invoked with the arguments. For example, (MULT (ADD 2 3) (ADD 1 4 2)) has a value of 35, since (ADD 2 3) has a value of 5, (ADD 1 4 2) has a value of 7, and (MULT 5 7) has a value of 35. Some functions have an arbitrary number of arguments; others require a fixed number. All statements return a value, which is either an atom or a list.

SET We may assign values to variables using the function SET. For example, the statement (SET ’test ’6) would have a value of a 6, would also cause the atom “test” to be bound to the atom “6”. The quote in front of the arguments indicates that the arguments should not be evaluated before the function is invoked. The quote in front of numbers is optional.

The function SETQ is the same as SET, but it causes LISP to act as if the first argument was quoted. The function EVAL returns the value of its argument, after it has been evaluated. (SETQ z ’(ADD 2 3)) has a value of the list (ADD 2 3) (EVAL ’z) has a value of (ADD 2 3) (EVAL z) has a value of 5 (but the binding of the atom z has not changed). o z is “resolved” twice: once because it is an argument to a function and LISP evaluates all arguments to functions before the function is invoked, once when the function EVAL is invoked to resolve arguments. The function ATOM can be used to tell whether an item is an atom or a list.

CAR, CDR and CONS The function (CAR x) returns the first item of the list x (and x must be a list or an error will occur); (CDR x) returns the list without its first element (again, x must be a list). The function CONS takes two arguments, of which the second must be a list. It returns a list which is composed by placing the first argument as the first element in the second argument’s list. The function REVERSE returns a list which is its arguments in reverse order.

DEF own functions (DEF SECOND(parms) (CAR (CDR parms))) defines a new function called SECOND which operates on a single parameter named “parms”. SECOND will take the CDR of the parameter and then the CAR of that result (SECOND ’(a b c d e)) would first CDR the list (yielding (b c d e)) and then CAR the result. Value would be the character “b”.

Consider the following program fragment: (SETQ X ’(a c s l)) (DEF WHAT(parms) (CONS parms (REVERSE (CDR parms)))) (DEF SECOND(parms) (CONS (CAR (CDR parms)) NIL))

Example Problems Evaluate: (CDR ’((2 (3))(4 (5 6) 7))) The CDR function takes the first element of its parameter (which is assumed to be a list) and returns the modified list. The first element of the list: ((2 (3))(4 (5 6) 7)) is (2 (3)), and the list without this element is ((4 (5 6 ) 7)).

Consider the following program fragment: (SETQ X ’(RI VA FL CA TX)) (CAR (CDR (REVERSE X))) What is the value of the CAR expression? The first statement binds variable X to the list (RI VA FL CA TX). The REVERSE of this list is the list (TX CA FL VA RI). whose CDR is (CA FL VA RI) The CAR of this list is just the atom “CA” (without the quotes).

Given the function definitions for HY and FY as follows: (DEF HY(PARMS) (REVERSE (CDR PARMS))) (DEF FY(PARMS) (CAR (HY (CDR PARMS)))) What is the value of the following? (FY ’(DO RE (MI FA) SO)) To evaluate (FY ’(DO RE (MI FA) SO)), we must first evaluate (CAR (HY (CDR ’(DO RE (MI FA) SO)))) Thus, HY is invoked with PARMS= ’(RE (MI FA) SO), and we evaluate (REVERSE (CDR ’(RE (MI FA) SO) )) This has a value of (SO (MI FA) ) which is returned to FY. FY now takes the CAR of this.

Evaluate the following expression. (EXP (MULT 2 (SUB 5 (DIV (ADD 5 3 4) 2)) 3) 3) (EXP (MULT 2 (SUB 5 (DIV 12 2)) 3) 3) (EXP (MULT 2 (SUB 5 6) 3) 3) (EXP (MULT 2-1 3) 3) (EXP –6 3) -216