Multiple Returns and Extra Parameters “LISP is unique in its capability to return more than one value (or no value at all) from a form. This neatly avoids.

Slides:



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

Modern Programming Languages, 2nd ed.
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 and Paradigms Lisp Programming.
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. 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.
Common Lisp! John Paxton Montana State University Summer 2003.
Returning values from functions You can return a value from a function by using the built- in function : ( return-from Function_name value) For example:
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 -
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
How to define functions? (DEFUN function-name parameter-list commands ) Example: (DEFUN myFunction (x y) (/ x y) ) To call this function, you would then.
TES3111 October 2001 Artificial Intelligence LISP.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Document Type Definitions. XML and DTDs A DTD (Document Type Definition) describes the structure of one or more XML documents. Specifically, a DTD describes:
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.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
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.
PRACTICAL COMMON LISP Peter Seibel 1.
PRACTICAL COMMON LISP Peter Seibel 1.
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
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.
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.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
04 Control. Control-Blocks Common Lisp has 3 basic operators for creating blocks of code progn block tagbody If ordinary function calls are the leaves.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Python Functions.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
CSE (c) S. Tanimoto, 2002 AI Techniques 1 Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Bindings Scope Extent.
Control in LISP More on Predicates & Conditionals.
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.
Structures “The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Chapter SevenModern Programming Languages1 A Second Look At ML.
XP 1 XSLT II Robin Burke ECT 360. XP 2 Homework #4 Forgot we hadn't covered "choose" becomes extra credit My books.xml didn't include descriptions.
Basic Introduction to Lisp
Functional Programming “When I code in C, I feel I'm on a knife-edge of ‘state’ — I focus on statements and what they do. I'm worried about the behaviour.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
PRACTICAL COMMON LISP Peter Seibel 1.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
Comparative Programming Languages Functional programming with Lisp/Scheme.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
Modern Programming Languages Lecture 20 Fakhar Lodhi
J.E. Spragg Mitthögskolan 1997
FP Foundations, Scheme In Text: Chapter 14.
Imperative Data Structures
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Modern Programming Languages Lecture 20 Fakhar Lodhi
Clojure Macros.
Defining Functions with DEFUN
Peter Seibel Practical Common Lisp Peter Seibel
Abstraction and Repetition
Common Lisp II.
Lisp.
LISP primitives on sequences
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

Multiple Returns and Extra Parameters “LISP is unique in its capability to return more than one value (or no value at all) from a form. This neatly avoids the problems which other languages are prone to when more than one object is produced or affected by a function.” -Star Sapphire Common Lisp Refernce Manual

Returning Multiple Values Any mathematical function returns exactly one value, and that’s usually true in LISP The exception to this is values (values S-expression*) Takes an arbitrary number of return values and returns all of the separately

Floor, Ceiling >(floor 10.5) >(+ (floor 10.5) 7) 17 >(ceiling 3.2) >(+ (ceiling 3.4) (floor 2.8)) 6 Extra return values are ignored if they aren’t expected

Returning with Values >(defun print-and-return-nothing () (format t "Hi") (values)) PRINT-AND-RETURN-NOTHING >(print-and-return-nothing) Hi >(values) >(values 1 (car '(a b)) 2) 1 A 2

Receiving Multiple Values multiple-value-bind operates like let, except local variables are initialized by the multiple return values If there are more variables than values, those left over will be nil If there are more values than variables, the extra values are discarded

Multiple-Value-Bind Examples >(multiple-value-bind (x y z) (values 1 2 3) (list x y z)) (1 2 3) >(multiple-value-bind (x y z) (values 1 2) (list x y z)) (1 2 NIL)

Other Multiple Value Functions multiple-value-call applies a given function to the multiple values returned by some function >(multiple-value-call #'+ (values 1 2 3)) 6 Multiple-value-list collects the values returned into a list >(multiple-value-list (values 1 2 3)) (1 2 3)

Get-Decoded-Time Returns multiple values indicating the current data and time >(get-decoded-time) NIL 5 Seconds Minutes Hours Day Month Year Day of Week Daylight Savings Time Zones West of GMT

Unlimited Arguments Some functions take an indefinite number of arguments ( +, *, etc.) To indicate a parameter that may occur zero or more times, put &rest in front of it The variable following &rest will be bound to a list of the remaining arguments

&rest Example >(defun test-rest (a b &rest c) (list a b c)) TEST-REST >(test-rest 'a 'b 'c) (A B (C)) >(test-rest 5 6) (5 6 NIL) >(test-rest 'a 'b 'foo 'y 60) (A 56 (27 B FOO Y 60))

Optional Argument An optional argument is one that may or may not be present Indicated with &optional Can specify in three ways – name : defaults to nil – (name default) : defaults to default – (name default supplied-flag) : defaults to default, and supplied-flag is t if given, nil otherwise

&optional Example >(defun test-optional (a &optional b (c 3) (d 4 e)) (list a b c d e)) TEST-OPTIONAL >(test-optional 1) (1 NIL 3 4 NIL) >(test-optional 1 2) ( NIL) >(test-optional 1 2 6) ( NIL) >(test-optional ) ( T)

Keyword Arguments (1) A keyword is a symbol whose first character is colon ( : ) Can’t have values changed Predicate for keywords is keywordp Indicated with &key

Keyword Arguments (2) Three ways to specify keyword arguments – name : called with :name, default is nil – (name default) : defaults to default – (name default supplied-flag) : as with optional parameters In all of the above, name can be replaced with (:keyword name), where :keyword is used in the call, and name within the function definition

&key Example >(defun test-key (a &key b (c 3) (d 4 e) ((:key f) 5)) (list a b c d e f)) TEST-KEY >(test-key 1) (1 NIL 3 4 NIL 5) >(test-key 1 2) Error >(test-key 1 :b 2) ( NIL 5) >(test-key 1 :c 12) (1 NIL 12 4 NIL 5) >(test-key 1 :d 14) (1 NIL 3 14 T 5) >(test-key 1 :key 30) (1 NIL 3 4 NIL 30) >(test-key 1 :key 17 :d 14 :b 13 :c 15) ( T 17)

Combining &rest, &optional and &key Required arguments first &optional must precede &rest When combining, the results can be counterintuitive >(defun test-together (a &optional b (c 3) &rest d &key e) (list a b c d e)) TEST-TOGETHER >(test-together 10 :e 13 :e 14) (10 :E 13 (:E 14) 14) >(test-together :e 13 :e 14) ( (:E 13 :E 14) 13)