Object-Oriented Programming In Lisp. Lisp vs. Scheme 1.Lisp has much more built-in functions and special forms, the Scheme language definition takes 45.

Slides:



Advertisements
Similar presentations
Introduction to LISP Programming of Pathway Tools Queries and Updates.
Advertisements

Functions Robin Burke CSC 358/
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.
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)
Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each.
Generics "It is easy to study the rules of overloading and of templates without noticing that together they are one of the keys to elegant and efficient.
Imperative Programming. Back to scheme Scheme is a functional language In some cases there is a need to capture objects state E.g. bank account.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Prof. Fateman CS 164 Lecture 151 Language definition by interpreter, translator, continued Lecture 15.
Alok Mehta - Programming in Lisp - Lecture Programming in Lisp Lecture 6 - Structure; Case Study: Blocks World.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Using Classes Lisp also supports the Object-Oriented paradigm. The process of defining classes in Lisp is similar to how you you define structures. The.
CSE S. Tanimoto Macros 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
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)
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Common Lisp Object System (CLOS)
PRACTICAL COMMON LISP Peter Seibel 1.
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.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
Def D OC An extensible, dynamic document creation system By Rahul Jain
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Data Abstraction and Mapping.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Introduction to Lisp For Scheme Users. What Makes Lisp Different? Built-in Support for Lists Automatic Storage Management Dynamic Typing First-Class Functions.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.
Lecture 1-2CS251: Intro to AI/Lisp II “And now for something completely different…”
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
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,
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
Review of objects  overview overview. Class of objects  Attributes/ methods.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Design of Problem Solvers (PS) using Classical Problem Solving (CPS) techniques Classical Problem Solver has 2 basic components:  Search engine (uses.
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)
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
Lists CSC 358/ Outline Lab #1 / Homework #1 Lists A question List Internals of Lists List operations Extended Example.
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
1 COSC generating functions, templates, and macros Yves Lespérance Adapted from Peter Roosen-Runge.
Basic Introduction to Lisp
Macros and general code walkers in Lisp: how useful! or, how useful? Ernst van Waning
Cs3180 (Prasad)L156HOF1 Higher-Order Functions. cs3180 (Prasad)L156HOF2 Equivalent Notations (define (f x y) (… body …)) = (define f (lambda (x y) (…
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
Operational Semantics of Scheme
Functions CSC 358/
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
Modern Programming Languages Lecture 20 Fakhar Lodhi
Allegro CL Certification Program
6.001 SICP Object Oriented Programming
(defmacro (delay x) (list 'lambda () x))
CLOS CSC 358/
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Functions and Macros.
Dynamic Scoping Lazy Evaluation
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Lisp: Using Functions as Data
Defining Macros in Lisp
Peter Seibel Practical Common Lisp Peter Seibel
Lisp: Using Functions as Data
Common Lisp II.
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

Object-Oriented Programming In Lisp

Lisp vs. Scheme 1.Lisp has much more built-in functions and special forms, the Scheme language definition takes 45 pages while Common Lisp takes 1029 pages) 2.Apart from lexical variables Lisp also has special variables 3.Scheme uses one name space for functions, variables, etc., Lisp doesn’t. 4.Scheme evaluates the function part of a function call in exactly the same way as arguments, Lisp doesn’t. 5.Lisp functions can have rest, optional and keyword parameters. Scheme functions only can have the equivalent of a rest parameter.

(defstruct account (name "") (balance 0.00) (interest-rate.06)) (defun account-withdraw (account amt) "Make a withdrawal from this account." (if (<= amt (account-balance account)) (decf (account-balance account) amt) 'insufficient-funds)) (defun account-deposit (account amt) "Make a deposit to this account." (incf (account-balance account) amt)) (defun account-interest (account) "Accumulate interest in this account." (incf (account-balance account) (* (account-interest-rate account) (account-balance account))))

(defun new-account (name &optional (balance 0.00) (interest-rate.06)) "Create a new account that knows the following messages:" #'(lambda (message) (case message (withdraw #'(lambda (amt) (if (<= amt balance) (decf balance amt) 'insufficient-funds))) (deposit #'(lambda (amt) (incf balance amt))) (balance #'(lambda () balance)) (name #'(lambda () name)) (interest #'(lambda () (incf balance (* interest-rate balance)))))))

> (setf acct (new-account "J. Random Customer" )) > (send acct 'withdraw ) => > (send acct 'deposit ) => > (send acct 'name) => "J. Random Customer" > (send acct 'balance) =>

(defun get-method (object message) "Return the method that implements message for this object." (funcall object message)) (defun send (object message &rest args) "Get the function to implement the message, and apply the function to the args." (apply (get-method object message) args)) (defun withdraw (object &rest args) "Define withdraw as a generic function on objects." (apply (get-method object 'withdraw) args))

(defmacro define-class (class inst-vars class-vars &body methods) "Define a class for object-oriented programming." ;;Define constructor and generic functions for methods `(let,class-vars (mapcar #'ensure-generic-fn ',(mapcar #'first methods)) (defun,class,inst-vars #'(lambda (message) (case #'make-clause methods)))))) (defun make-clause (clause) "Translate a message from define-class into a case clause." `(,(first clause) #'(lambda,(second clause).,(rest2 clause)))) (defun ensure-generic-fn (message) "Define an object-oriented dispatch function for a message, unless it has already been defined as one." (unless (generic-fn-p message) (let ((fn #'(lambda (object &rest args) (apply (get-method object message) args)))) (setf (symbol-function message) fn (get message 'generic-fn) fn)))) (defun generic-fn-p (fn-name) "Is this a generic function?" (and (fboundp fn-name) (eq (get fn-name 'generic-fn) (symbol-function fn-name))))

(define-class account (name &optional (balance 0.00)) ((interest-rate.06)) (withdraw (amt) (if (<= amt balance) (decf balance amt) 'insufficient-funds)) (deposit (amt) (incf balance amt)) (balance () balance) (name () name) (interest () (incf balance (* interest-rate balance)))) (setf acct2 (account "A. User" )) > (deposit acct ) => > (interest acct2) => > (balance acct2) => > (balance acct) =>

(define-class password-account (password acct) () (change-password (pass new-pass) (if (equal pass password) (setf password new-pass) 'wrong-password)) (otherwise (pass &rest args) (if (equal pass password) (apply message acct args) 'wrong-password))) (setf acct3 (password-account "secret" acct2)) > (balance acct3 "secret") => > (withdraw acct3 "guess" ) => WRONG-PASSWORD > (withdraw acct3 "secret" ) =>

(defclass account () ((name :initarg :name :reader name) (balance :initarg :balance :initform 0.00 :accessor balance) (interest-rate :allocation :class :initform.06 :reader interest-rate))) (defmethod withdraw ((acct account) amt) (if (< amt (balance acct)) (decf (balance acct) amt) 'insufficient-funds)) (setf a1 (make-instance 'account :balance :name "Fred")) > (name a1) => "Fred" > (balance a1) => > (interest-rate a1) => 0.06

(defclass limited-account (account) ((limit :initarg :limit :reader limit)))) (defmethod withdraw ((acct limited-account) amt) (if (> amt (limit acct)) 'over-limit (call-next-method)))) (setf a2 (make-instance 'limited-account :name "A. Thrifty Spender" :balance :limit )) > (name a2) => "A. Thrifty Spender" > (withdraw a ) => OVER-LIMIT > (withdraw a ) => 480.0

(def