Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Advanced LISP Functions: Optional Parameters, Functional Arguments, and Mapping Functions , Study notes of Electrical and Electronics Engineering

Lisp notes on advanced functions, including the use of optional parameters with the &optional keyword, functional arguments requiring special processing with funcall and apply, and mapping functions like mapcar and related primitives. Examples and templates are given for each concept.

Typology: Study notes

Pre 2010

Uploaded on 03/10/2009

koofers-user-lrs-1
koofers-user-lrs-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download Advanced LISP Functions: Optional Parameters, Functional Arguments, and Mapping Functions and more Study notes Electrical and Electronics Engineering in PDF only on Docsity! EEL-5840 Dr. A. A. Arroyo Fall 2008 Advanced LISP Functions Lisp Notes 2 1 Optional Parameters in LISP We can elegantly define procedures that include optional parameters in LISP by using the &OPTIONAL keyword in the argument list of a function definition. By enclosing the parameter(s) that follow the &optional keyword in parentheses one can supply a default value (otherwise they are automatically defaulted to nil). More than one parameter can follow the &optional keyword. template: (defun fname (arg1 arg2 &optional arg3-argn) (<body>) ) Example: Write a function to count the top-level elements of a list (defun count-top (lis &optional (answer 0)) (if (endp lis) answer (count-top (cdr lis) (+ 1 answer)) )) Note this version eliminates the need for a helper function (defun count-top (lis) (count-help lis 0)) (defun count-help (lis answer) (if (endp lis) answer (count-help (cdr lis) (+ 1 answer)) )) Other Types of Parameters in LISP The &REST parameter in LISP is bound to a list of all otherwise accounted for argument values. Using this we could define a multiple list append as follows: (defun manyappend (&rest lists) (append-help lists)) (defun append-help (lists) (if (endp lists) nil (append (car lists) (append-help (cdr lists))) )) Keyword parameters are designated by &KEY to allow for the easy identification of many parameters and to bypass positional binding. Using this we can write two versions of member, one using eq the other using equal. (defun MYMEMBER (SEX LIS &KEY TESTFCN) (COND ((NULL LIS) NIL ) ((IF (NULL TESTFCN) (EQ SEX (CAR LIS)) (TESTFCN SEX (CAR LIS))) LIS ) ( T (MYMEMBER SEX (CDR LIS) :TESTFCN TESTFCN)) )) EEL-5840 Dr. A. A. Arroyo Fall 2008 Advanced LISP Functions Lisp Notes 2 2 Functional Arguments Unfortunately this version looks good but it will not run! The LISP reader interprets TESTFCN as the actual name of a function (which it does not have) rather than a variable that represents the actual name of the function. Functional arguments (or parameters) are called funargs and demand special processing. The system supplies two functions to deal with funargs: FUNCALL and APPLY. template: (funcall #'<fname> <arg1> <arg2> ... <argN>) (car '(a b c)) == (funcall #'car '(a b c)) (append '(a b) '(c d)) == (funcall #'append '(a b) '(c d)) Apply works similarly, by using its first argument which must be a funarg on the elements of its second argument value, a list. template: (apply #'<fname> <list of arguments>) (car '(a b c)) == (apply #'car '((a b c))) (append '(a b) '(c d)) == (apply #'append '((a b) (c d))) Using funcall we can fix MYMEMBER as follows: (defun MYMEMBER (SEX LIS &KEY TESTFCN) (COND ((NULL LIS) NIL ) ((IF (NULL TESTFCN) (EQ SEX (CAR LIS)) (FUNCALL TESTFCN SEX (CAR LIS))) LIS ) ( T (MYMEMBER SEX (CDR LIS) :TESTFCN TESTFCN)) )) MAPping Functions The MAPCAR and related primitive functions allow us to easily transform lists. The function takes two arguments, the first is a funarg and the second a list. The answer is a list of the result of applying the functional argument to each successive car of the second argument. For example (mapcar #'f '(x1 x2 x3)) ==> (f(x1) f(x2) f(x3)) We can define mapcar as follows: (defun mapcar (f lis) (cond ( (null lis) nil ) ( t (cons (funcall f (car lis)) (mapcar f (cdr lis))) )) (mapcar #'oddp '(1 2 3)) ==> (T NIL T) Let us write substop, a function that substitutes sex1 for sex2 at the top-level of lis, a list of elements. (defun replacesex (sex3) (if (equal sex1 sex3) sex2 sex3)) (defun substop (sex1 sex2 lis) (mapcar #'replacesex lis)) /* This code will not run */ If one tries this code, it fails because sex1 is unbound. The problem is resolved using lambda expressions below.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved