1 Programming Languages and Paradigms Lisp Programming.

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.
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.
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.
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)
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.
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.
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.
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)
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.
Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.
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.
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.
Common lisp A functional programming language. Useful URL:
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
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
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
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.
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.
Comparative Programming Languages Functional programming with Lisp/Scheme.
EZGİ GENÇ History Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally.
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
Section 15.4, 15.6 plus other materials
CS 550 Programming Languages Jeremy Johnson
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
LISP LISt Processing.
LISP A brief overview.
Using Lisp Lisp is a interactive system
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
First Lecture on Introductory Lisp
Modern Programming Languages Lecture 21 Fakhar Lodhi
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
CS 36 – Chapter 11 Functional programming Features Practice
Modern Programming Languages Lecture 20 Fakhar Lodhi
Lisp and Scheme I.
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Defining Functions with DEFUN
LISP LISt Processing.
Abstraction and Repetition
LISP LISt Processing.
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Programming Languages
Lisp.
Presentation transcript:

1 Programming Languages and Paradigms Lisp Programming

2 Brief Intro Lisp: List Processor Designed in 1958 by McCarthy (2 nd oldest programming language) Functional programming language Interpreted Based on symbolic expressions, lists, functions, recursion

3 Symbols and Numbers Symbol String of characters (letters, digits, and hyphens) Examples: x Move a1 turn-right SQR NOT case sensitive Number Examples: e E-23 Just like int or double constants in C/Java

4 Lists List: Sequence of symbols, numbers, or lists Examples: (a b c d e 1 2 3) (This list (contains (4 elements)) (really)) Expressions that aren’t lists are atoms Examples: A 1 the The empty list is nil nil is a special symbol both a list and an atom

5 Lisp Expressions and the Lisp Interpreter The interpreter repeatedly: Prompts for a well-formed expression Evaluates the expression Returns a response Examples: > (+ 1 5)> (square 5) 625 > ’(square 5)> (first ’((a b) c (1 2) 3) (square 5)(a b)

6 Built-in Functions Numeric Functions List Access Functions List Construction Functions Predicates quote and setq defun Special Functions: if, cond, loop

7 Numeric Functions Example: ( ) + - * / sqrt expt min max abs mod round sin cos tan

8 List Access Functions first or CAR: returns the first element of its argument list rest or CDR: returns a list containing all but the first element of a list last: returns the last element (as a list) of a list length: returns the number of elements in a list

9 List Construction Functions cons: takes two arguments; returns the result of inserting the first argument in front of the second argument (opposite of car) append: takes two list arguments; returns a concatenation of the two lists list: returns a list of all its arguments

10 Predicates listp numberp integerp stringp atom NOTE: nil is false, T is true null: checks if the argument is nil = equal eq eql and or not

11 quote quote or ’ prevents an expression from being evaluated (quote exp) same as ’exp > a Error because a is unbound/can’t be evaluated >’a a >(+ 3 2) 5 >’(+ 3 2) (+ 3 2)

12 setq setq stores a value for a symbol >(setq a 5) 5 >a 5 >(setq b a) 5 >(setq c ’a) a >(setq acts ’(s l r)) (s l r)

13 Evaluating a symbol >’a a >(setq a 5) 5 >a 5 >’a a

14 defun (defun func-name (args) body ) body may contain some elements in args body may contain several expressions Last expression is the one returned

15 Special Functions (if condition then-result else-result) (cond (test1 result1) (test2 result2) … ) You would often have a final condition that captures all remaining cases (T (whatever …)) (loop …)

16 load Create a text file containing Lisp expressions Suppose the file is named file.lisp Type in the expression: > (load ’file.lisp)

17 Some Examples Define functions that Computes the square of its argument Computes the absolute value of its argument Computes n! Reverses the elements in a list Flattens a list (removes nested lists) For the last 3 problems, use recursion instead of iteration

18 square and myabs (defun square(n) (* n n)) ; abs is already defined as a builtin (defun myabs(n) (if (< n 0) (- n) n ) )

19 factorial Recurrence: n! = 1 if n = 0 n*(n-1)! otherwise (defun factorial(n) (if (= n 0) 1 (* n (factorial (- n 1 ))) ) )

20 The rev function (reverse is already defined as a built-in) Recurrence: If nil, return nil Otherwise, append the reverse of the rest (cdr l) with a list containing the first (list (car l))

21 The flatten function Three cases: nil, (car l) is an atom, or (car l) is a list Recurrence: If nil, return nil If (car l) is an atom, insert (car l) into (flatten (cdr l)) If (car l) is a list, append (flatten (car l)) and (flatten (cdr l))