Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,

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.
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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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)
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.
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 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 יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
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.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
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.
ISBN Chapter 15 Functional Programming Languages.
Programming Languages I LISP
Rahman Lavaee Mashhadi Mohammad Shadravan. Conditional expressions LISP was the first language to contain a conditional expression In Fortran and Pascal.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
ISBN Chapter 15 Functional Programming Languages.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
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.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
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.
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.
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.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
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.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Functional Programming Languages
Functional Programming
Modern Programming Languages Lecture 20 Fakhar Lodhi
Introduction to Scheme
Chapter 15 – Functional Programming Languages
LISP LISt Processing.
Functional Programming Languages
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.
Functional Programming Languages
CS 36 – Chapter 11 Functional programming Features Practice
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP LISt Processing.
Abstraction and Repetition
Functional Programming: Lisp
LISP LISt Processing.
15.2 Mathematical Functions
Common Lisp II.
Programming Languages
Lisp.
LISP primitives on sequences
Presentation transcript:

Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive, and semantically elegant language

Pure Functions Pure functions avoid “side effects” –No use of global variables –Do not change parameters –Return one thing Obeys principle of referential transparency “Function using same arguments will always produce same value”

Function examples plustwo(x) ::= plusone (plusone(x)) where plusone(x) ::= x+1 fact(n) ::= if n = 0 then 1 else n * fact(n-1)

Lisp’s base language Pure functions, operations, functional composition, recursion, and conditional expression Basics of Lisp –Domain language operates on S-expr –S-expr constructed out of atoms and parentheses (list of atoms)

Basics… Atom (primitive data structure) –Indivisible –Numeric, non-numeric (some versions string) –Sequence of alphabetic, numeric characters – ATOM four –Equality is only built-in operation S-expr –Atom: 876 four –List: (ADD 2 PLUS 3) ((Ruth a) (Edward b)) (TIMES Y Z)

Lists and Operators Lists can be versatile –Can be composed of constants, variables, and operators Built- in binary functions for arithmetic exist (plus 2 3) or (+ 2 3) ( )

Conditions (cond ( (null x) 0) ( (eq x y) (f x) ) ( T (g y) ) ) if null (x) then 0 elseif x == y then f(x) else g(y) endif;

How to construct a data Structure Only one – the list (to be or not to be) has 6 atoms cons – can put atoms to atoms together to make a list car – extract atoms from a list

Primitive Functions car – returns the first S-expr in a list car (A)  A car (A B)  A car ((A B) (C D))  (A B) car ATOM  undefined cdr – returns a list containing everything but the first S-expr in a list cdr (A)  NIL cdr (A B)  (B) cdr ((A B) (C D))  ((C D)) cdr ATOM  undefined

Composition of Functions L = ( (AB) (C) (D E F)) car (car (cdr (L)))  car(car( (C) (D E F)))  car ( C)  C

Another Primitive Functions cons – construct (or concatenate) cons (A; (B C) )  (A B C) cons ( (A); (B C) )  ( (A) B C) –construct s-expr z, such that car (z) = x and cdr (z) = y

User-defined functions Everything is a function in LISP and program functions are lists Use defun –Call function “defun” with 3 arguments Function name, formal parameter list, body (defun double (N)(double 5)  10 (* N 2) )

(make-table text nil) Q: Is this 3 atoms or a function call with two parameters? A: function call assuming defined function make-table If you want it to be treated as 3 atoms use a single- quote ‘ (make-table text nil)

mapcar Code iterative functions mapcar takes 2 arguments –Function name –A list Returns a list Applies a function to each item in a list and returns resulting values in a new list

(defun double-list (lis) (mapcar (function double) lis)) or (defun double-list (lis) (mapcar ‘ double lis) --the function name is an argument to mapcar function

mapcar… if lis = ( ) before (double-list lis) or (double ‘ ( )) then lis = ( ) after

lambda expression Serve the same purpose as a “nested function” Anonymous functions –Define a function within a function –But function has no name

Do not evaluate within mapcar, DEFINE (defun double-list (lis) (mapcar ‘ (lambda (N) (* N 2 ) ) lis) ) –Almost identical to “helping function” or subfunction –defun and function name replaced by lambda, which has a parameter list and function body –However appears in middle of another function; –No way to call lambda function from any function except from one in which it is embeded

More examples (lambda (x) (+ x 5)) (lambda (x y) (+ (* 2 x ) (* 3 y))) (lambda (x y z) (cond (eq x NIL) y) (T z) ) ) Variables x, y and z are bound variables – local to lambda expression

May have unbound variables occurring in lambda expressions (lambda (x) (+ x z)) Here z is an unbound variable Q: Where does it get its value when lambda expression is evaluated? A: outer function that lambda is nested inside

(defun MultList (z lis) (mapcar # ’ (lambda (x) (+ x z) ) lis ) ) Treat variables like z inside lambda expression as local variables from definition

Other Statements Reading (read) returns whatever typed in (be it a list or an atom) (setq list (read)) I’ll type in (A B C D) or (setq Item (read)) I’ll type in A

Local variables in a function? Use let (defun fnName (parameters) (let ( (list nil) … (n 1) ( ) )

Set members Member of a set (setq oddset ‘( ) ) (member 5 oddset)  returns T or F

Append and Reverse (append list-A list-B)  returns a list (reverse (concatenate ‘ string “ABC” “DEF”)

Setup and Running Download CLISP - Brief introduction to install and setup of an artificially intelligent environment (YouTube video)CLISP - Brief introduction to install and setup of an artificially intelligent environment Demo