CSE 341, S. Tanimoto Lisp 3 - 1 Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.

Slides:



Advertisements
Similar presentations
Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn.
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.
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.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
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.
Introduction CSC 358/458 3/27/2006. Outline Introductions Course goals Course organization History of Lisp Lisp syntax Examples Lisp IDE.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
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.
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.
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,
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.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
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.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CSE S. Tanimoto More-Concepts - 1 More Programming Language Concepts Currying Lazy Evaluation Polymorphism.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Functional Programming: Lisp MacLennan Chapter 10.
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Section 15.4, 15.6 plus other materials
Lisp S-Expressions: ATOMs
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
First Lecture on Introductory Lisp
Writing LISP functions
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Scheme: Basic Functionality
Abstraction and Repetition
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Explicit Application of Procedures, and Explicit Evaluation
Functional Programming Concepts
Lisp: Using Functions as Data
LISP A brief overview.
Functional Programming Concepts
Lisp: Representation of Data
Lecture #7 מבוא מורחב.
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Lisp: Using Functions as Data
Bindings, Scope, and Extent
LISP: Basic Functionality
Functional Programming Concepts
LISP: Basic Functionality
Bindings, Scope, and Extent
Common Lisp II.
LISP: Basic Functionality
Lisp: Representation of Data
LISP primitives on sequences
Lists in Lisp and Scheme
Abstraction and Repetition
Presentation transcript:

CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures and macros). Non-built-in Functions are usually defined using the special form DEFUN. > (defun sqr (x) (* x x)) SQR > (defun sqr (y) ”Returns the square of its arg." (* y y) ) SQR

CSE 341, S. Tanimoto Lisp Functions with no arguments A function can take zero, one, or more arguments. > (defun say-hello () "prints a greeting." (format t "Hello there!~%")) SAY-HELLO > (say-hello) Hello there! NIL >

CSE 341, S. Tanimoto Lisp Symbol binding, function property A symbol can have a value and a function binding at the same time. > (setq foo 10) 10 > (defun foo (n) " Returns n plus two. " (+ n 2) ) FOO > (describe 'foo) FOO is a SYMBOL. Its value is 10 It is INTERNAL in the COMMON-LISP-USER package. Its function binding is # The function takes arguments () Its property list has these indicator/value pairs: EXCL::%FUN-DOCUMENTATION "Returns n plus two." >

CSE 341, S. Tanimoto Lisp COND (an alternative to IF) > (defun small-prime-p (n) "Returns T if n is a small prime." (cond ((= n 2) t) ((= n 3) t) ((= n 5) t) ((= n 7) t) (t nil) ) ) SMALL-PRIME-P > (small-prime-p 9) NIL > (small-prime-p 3) T

CSE 341, S. Tanimoto Lisp Recursive Functions A function may be defined in terms of itself. > (defun factorial (n) "Returns factorial of N." (if (= n 1) 1 (* n (factorial (- n 1))) ) ) FACTORIAL > (factorial 5) 120 > (factorial 20)

CSE 341, S. Tanimoto Lisp Primitive List-Manipulating Functions > (setq x '(a b c d)) (A B C D) > (first x) ; FIRST returns 1st elt of list A > (rest x) ; REST returns all but 1st. (B C D) > x (A B C D) ; X has not been changed. > (cons 'a 'b) (A. B) ; CONS combines two things. > (cons 'a nil) (A) ; The result is often a list. > (cons 'z x) (Z A B C D)

CSE 341, S. Tanimoto Lisp CAR, CDR, and their combinations > (setq x '(a b c d)) (A B C D) > (car x) A ; CAR is equivalent to FIRST > (rest x) (B C D) ; CDR is equivalent to REST. > (cdr (cdr x)) (C D) > (cddr x) (C D) > (caddr x) C ; CADDR is equivalent to THIRD

CSE 341, S. Tanimoto Lisp Recursive Functions of Lists > (defun censor (lst) "Returns LST with no instances of BAD." (cond ((null lst) nil) ((eq (car lst) 'BAD) (censor (rest lst)) ) (t (cons (car lst) (censor (rest lst)) ) ) ) ) CENSOR > (censor '(This is a bad bad list)) (THIS IS A LIST)

CSE 341, S. Tanimoto Lisp One-Way Recursion Doesn’t Do Sublists > (censor '(This bad list (has a bad sublist))) (THIS LIST (HAS A BAD SUBLIST))

CSE 341, S. Tanimoto Lisp Two-Way Recursive Functions > (defun censor2 (lst) "Returns LST with no instances of BAD." (cond ((null lst) nil) ((atom lst) lst) ((eq (car lst) 'BAD) (censor2 (rest lst)) ) (t (cons (censor2 (car lst)) (censor2 (rest lst)) ) ) ) ) CENSOR2 > (censor2 '(This bad list (has a bad sublist))) (THIS LIST (HAS A SUBLIST))

CSE 341, S. Tanimoto Lisp Looping: DOTIMES > (defun promise (n) "Prints a promise N times." (let ((the-promise "I will balance parentheses")) (dotimes (i n) (format t "~d. ~A.~%" i the-promise) ) ) ) PROMISE > (promise 3) 0. I will balance parentheses. 1. I will balance parentheses. 2. I will balance parentheses. NIL >

CSE 341, S. Tanimoto Lisp Looping: DOLIST > (defun print-on-separate-lines (lst) "Prints LST with one line per element." (dolist (elt lst nil) (print elt) ) ) PRINT-ON-SEPARATE-LINES > (print-on-separate-lines '(lunch around the corner) ) LUNCH AROUND THE CORNER NIL >

CSE 341, S. Tanimoto Lisp Pure Functions A pure function is one whose returned value depends only on its arguments (i.e., it possesses referential transparency), and it does not have any side effects. (defun plus3 (x)(+ x 3)) ; a pure function (defun plus3-with-y (x) ; not a pure function (setq y 3) (+ x y) ) (defun plus3-with-z (x) ; not pure unless (+ x z) ) ; z is constant

CSE 341, S. Tanimoto Lisp Functional Programming Pure functional programming is programming using only pure functions. No assignment statements, no loops.