Skip to content

Lambda Lists & Symbol Value

Ashok Khanna edited this page Feb 6, 2022 · 1 revision

20:16 lisp123 Practical Common Lisp on Lambda List Keywords: they must be declared in the order I've discussed them: first the names of the required parameters, then the optional parameters, then the rest parameter, and finally the keyword parameters. lisp123 Is this actually true? Bike sure is. lisp123 Interesting lisp123 (defun test (&key a &rest b) (print a) (print b)) & then (test :a 3 4 5 6) gave an error on 'unpaired keyword' Bike clhs 3.4.4 lists the grammar for ordinary lambda lists lisp123 I see, I didn't know it was in order (http://www.lispworks.com/documentation/lw60/CLHS/Body/03_da.htm) lisp123 So [ ... ] is always in order right when reading these syntax descriptions? Bike it's in a slightly modified BNF. [whatever] means optional. Bike the required order is just the order of the clauses. lisp123 Is there a particular reason why order is enforced? Bike the semantics don't make sense in any other order. lisp123 Why is that? Bike like, what would having &rest before &optional mean? lisp123 Thing is &rest collects all the items with keywords Bike What? lisp123 So I thought the order in defining the (ordinary) lambda list in the function definition wouldn't matter lisp123 but when calling it, it is more important Bike I don't understand what you mean by "collects all the items with keywords". lisp123 PCL: If both &rest and &key appear in a parameter list, then both things happen--all the remaining values, which include the keywords themselves, are gathered into a list that's bound to the &rest parameter, and the appropriate values are also bound to the &key parameters. Bike well, yes, but you can do &rest without also having &key. Bike &rest just gives you a list of all parameters subsequent to the required and optional ones. lisp123 I see Bike i suppose you could imagine allowing &rest and &key to be reordered while not changing the meaning, but what would be the point? makes code potentially a little harder to read for no obvious reason. lisp123 Yeah no point, was just curious because I was writing a parser for ordinary lambda lists lisp123 And I was going to store the order in which the lambda-list keywords appeared, but now don't have to tyson2 has left IRC (Read error: Connection reset by peer) lisp123 One more question, is &key &rest &optional - symbols? lisp123 (find-package (symbol-name &key)) & (find-package '&key) both returned nil lisp123 ignore me! lisp123 I wish there was recall in IRC!! lisp123 https://plaster.tymoon.eu/view/2924#2924 lisp123 Is there a way to get this to work? lisp123 I want to push to the variable stored in CURRENT-PARAMETER-TYPE, not to CURRENT-PARAMETER-TYPE itself lisp123 I get a bit closer with (symbol-value current-parameter-type) lisp123 But it then seems to refer to a global variable and not the local version lisp123 https://plaster.tymoon.eu/view/2925#2925 lisp123 Works lisp123 But does anybody have a better way? Bike why do you want to make all of these special variables, exactly? lisp123 (not so much on parsing the lambda list, but on storing pointers to local variables) lisp123 I added that hack to get symbol-value to work Bike oh, i see what you're doing now. i can't recommend writing your code like this. i'd do something like (case current-parameter-type ((required-parameters) (push item required-parameters)) ...) Bike or you could have a list or array structure or something instead of variables lisp123 Yeah, that's how I usually did it. Just tried something new lisp123 or you could have a list or array structure or something instead of variables -> thanks lisp123 that sounds like a better way lisp123 (when trying to avoid the case)