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:

Slides:



Advertisements
Similar presentations
09 Examples Functional Programming. Tower of Hanoi AB C.
Advertisements

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
ANSI Common Lisp 5. Control 16 June Blocks -- progn progn > (progn (format t “a”) (format t “b”) ( )) ab 23 The expressions within its body.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
Higher Order Functions “I hope you’re convinced, by now, that programming languages with first-class functions let you find more opportunities for abstraction,
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.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Arrays (1) You create an array in LISP by using the function (make- array ). All elements are initially set to nil. To create a 1-dimensional array of.
Project 1: Background Informed search : 8- puzzle world BFS, DFS and A* algorithms Heuristics : Manhattan distance, Number of misplaced tiles Lisp programming.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
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.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
Mapping And Iteration So far, the only Lisp mechanism we have considered, which allows an action to be performed repeatedly is recursion. Most programming.
How to load a program file? Lisp programs in Allegro are saved under the file extension.cl. To load a file into the Lisp console, use the following: (load.
How to define functions? (DEFUN function-name parameter-list commands ) Example: (DEFUN myFunction (x y) (/ x y) ) To call this function, you would then.
Logical functions and - Logical AND function. Example: (and (> 5 4) (< 5 4)) or - Logical OR function. Example: (and (> 5 4) (< 5 4)) not - Logical negation.
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.
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.
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
Functional Programming 02 Lists
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Data Abstraction and Mapping.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
PRACTICAL COMMON LISP Peter Seibel 1.
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.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
Common Lisp! John Paxton Montana State University Summer 2003.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
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.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
06 INPUT AND OUTPUT Functional Programming. Streams Two kinds of streams  Character streams  Binary streams Character streams are Lisp objects representing.
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
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.
Design of Problem Solvers (PS) using Classical Problem Solving (CPS) techniques Classical Problem Solver has 2 basic components:  Search engine (uses.
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.
Classification of Numbers Properties of Real Numbers Order of Operations R1 Real Numbers.
Search as a problem solving technique. Consider an AI program that is capable of formulating a desired goal based on the analysis of the current world.
Functional Programming: Lisp MacLennan Chapter 10.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Example of formula (defun roots (a b c) (list
CSC113: Computer Programming (Theory = 03, Lab = 01)
MATLAB: Structures and File I/O
Type Topic in here! Created by Educational Technology Network
Arrays .
CSE S. Tanimoto Explicit Function Application
Functional Programming Concepts
Lisp: Using Functions as Data
Functional Programming Concepts
Peter Seibel Practical Common Lisp Peter Seibel
Abstraction and Repetition
1-6 Midterm Review.
Lisp: Using Functions as Data
Functional Programming Concepts
Common Lisp II.
Lisp.
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
(Type Answer Here) (Type Answer Here) (Type Answer Here)
Presentation transcript:

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: (defun plus (x y) (setq answer (+ x y)) (return-from plus answer) ) (setq result (plus 10 20))

Block You can also group expressions together in a structure called block. For example: (block justablock (format t “ is ~D” ( )) ) A block is used to demarcate a group of expressions that achieves a certain result. You can return values from blocks just as you do with functions.

nil block The blocks in the previous slides are called named blocks. You can also have blocks which are labeled nil. For example, loops are default labeled nil. Here is another example of a nil block. (block nil (print “This is a nil block”) (return “End of block”) ) To return a value from a nil block, use (return value)

Funcall An example of this would be: (funcall #’+ 1 99) Notice that #’ is used to denote a function. In this case + is the required function. Funcall calls its first argument on its remaining arguments. This is an example of a function that takes another function as one of its arguments.

Apply An example of this function is as follows: (apply #’ ‘(5 6)) The above is similar to ( ) This is similar to funcall except that its final argument should be a list. The elements of that list are treated as additional arguments to funcall.

When to use these? These two functions are very useful when their first argument is a variable. This means you may have more than one function that is applicable on a set of arguments. For example, to find the shortest distance in a network, you may have several possible algorithms to perform the search. These algorithms are written as functions. Hence, you can use apply or funcall to specify the desired function (algorithm) to perform the search.

Mapcar This is another interesting function that takes a function of one argument and a list. It works by –applying the function to each element of the list, and –collects the results in a list and return them. For example, (mapcar #’sqrt ‘( )) (mapcar #’first ‘((a b) (c d) (e)))

Lambda If you want to create a temporary function and do not want to name it, then use lambda. For example: #’(lambda (x) (* 8 x)) To call the above defined lambda function, do as follows: (funcall * 5)