How to define functions? (DEFUN function-name parameter-list commands ) Example: (DEFUN myFunction (x y) (/ x y) ) To call this function, you would then.

Slides:



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

ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
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.
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.
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
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.
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
Commands and predicates LISP functions are divided into 2 classes. Predicates are functions that return boolean values i.e. t or nil. The rest are commands.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Example 2.
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.
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:
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
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.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. 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.
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
Digital Electronics Data Structures LISP
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.
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.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
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.
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.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Introduction to Scheme. Lisp and Scheme Lisp: List processor language –Full recursion –Conditional expression –Extensibility –Interactive Scheme: one.
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.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
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)
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.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Comparative Programming Languages Functional programming with Lisp/Scheme.
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.
Functional Programming
Functions CSC 358/
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP LISt Processing.
Method.
CS1061 C Prgramming Lecture 11: Functions
FP Foundations, Scheme In Text: Chapter 14.
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Lisp: Using Functions as Data
Defining Functions with DEFUN
LISP LISt Processing.
Abstraction and Repetition
Lisp: Using Functions as Data
LISP LISt Processing.
Modern Programming Languages Lecture 18 Fakhar Lodhi
Lisp.
Presentation transcript:

How to define functions? (DEFUN function-name parameter-list commands ) Example: (DEFUN myFunction (x y) (/ x y) ) To call this function, you would then type this (for example) : (myFunction 10 5) You do not have to declare the variable types!!

A Factorial function (DEFUN fact (x) (if (> x 0) (* x (fact (- x 1))) 1 ) The algorithm is as follows: if x > 0 then x * fact(x-1) else 1 This is an example of a recursive function!

Another function Here is another function: (defun tryThis (x) (setq x (* x 4)) (setq x (+ x 3 4) (+ x -10) ); end of function Note that a function with multiple statements in its body always return the value of its final statement.

Changing a list element The function below demonstrate how to change the second element of a list to another value. (DEFUN Change2nd ( list newvalue ) (cons (car list) (cons newvalue (cdr (cdr list)))) )

Optional arguments (1) You can specify that a particular argument (or arguments) are optional for a function as follows: (defun myFunc1 ( m &optional n ) (if n m 100) ) You can call this function with 1 or 2 arguments. If you supply 2 arguments, the function will return the value of m. If only 1 argument is supplied, the function returns 100.

Optional Arguments (2) This is another example (defun myFunc2 ( &optional (a 10) (b 20)) (* a b) ) This function has 2 optional arguments. Default values are specified for these arguments. If an optional argument is not included in the function call, the default values will be used.

Function with any number of arguments You can write a function that takes an unspecified number of arguments as follows: (defun anyfunction (x &rest y) y) (anyfunction 3) (anyfunction ) In this function, if you specify more than 1 argument, the second argument onwards will be stored in the list named rest.

Eql Eql(x y) Returns true only x and y if they are numbers of the same type with the same value, or if they are character objects that represent the same character. It is very similar to eq, except that it is more general. (eq ‘a ‘b)(eq ‘(a b) ‘(a b)) (eq ‘a ‘a) (eq 3 3.0) (eq #\a #\A) Eql is case and type sensitive.

Equal Equal is more general than Eql. Try this : (equal ‘a ‘b)(equal ‘(a b) (‘a b)) (equal ‘a ‘a) (equal 3 3.0) (equal #\a #\A) Now try the above expressions using Equalp. What are the differences??