Little bits of lisp - Function lambda lists

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

you can also give default values for arguments in terms of previous arguments. like so:
(defun my-cool-func (x &optional (y (* 2 x)) (z (+ x y)))
(list x y z))

CL-USER> (my-cool-func 3)
=>
(3 6 9)

CL-USER> (my-cool-func 3 2)
=>
(3 2 5)

you can do basically anything in these default-value fields.
for example, if the user doesn't provide an optional value, define a new function which adds the first argument to a given input.
this new function is only defined if the user does not pass the optional argument.
(defun my-cool-func (x &optional (adder-func (defun my-adder-func (input) (* input x))))
(funcall adder-func x))

CL-USER> (fboundp 'my-adder-func)
=>
NIL

CL-USER> (my-cool-func 2 (lambda (input) (+ input 10)))
=>
12

CL-USER> (fboundp 'my-adder-func)
=>
NIL

CL-USER> (my-cool-func 2)
=>
4

CL-USER> (fboundp 'my-adder-func)
=>
T

chijunky
Автор

Ditto on the praising the format. It helps to have the bite size break down of the topics, and your explanations are clear. Thanks.

histufly
Автор

There is also the verboten &aux for some odd looks from people. :) It's a vestigial thing that makes local symbols beforehand you can SETF in the function body later.

PixelOutlaw
Автор

If you can just paste all the codes in the description. This is going to help us all. Great work between. Thank you. I think you shouldn't combine optional and key. Rest is safe with both.

subramaniantr
Автор

Hello :) I was reading Practical Common Lisp and it says these about the combination of &optional with &rest:

> It's possible, but rare, to use all four flavors of parameters in a single function. Whenever more than one flavor of parameter is used, 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. Typically, however, in functions that use multiple flavors of parameters, you'll combine required parameters with one other flavor or possibly combine &optional and &rest parameters. The other two combinations, either &optional or &rest parameters combined with &key parameters, can lead to somewhat surprising behavior.

So it seems that the author finds the optional/rest combination valid?

(Love these brief videos by the way! Thanks! :D)

erfeyah
Автор

thank you, FINALLY i understand it! :D

ejsafara
Автор

great video ! please make video about reader macro :)

mochdeden
Автор

Thanks for these videos. They are really useful! Quick question: I am reading the SICP book and it is talking about streams as an implementation of state without actually storing state in a variable (section.3.5.5) . As it is quite basic and useful is this something that is part of the core of Common Lisp? If yes what is the syntax?

erfeyah
Автор

Hey Baggers, you probably get this question a lot, but here it is again. What do you think about scheme?

ricardo.mazeto
Автор

Does the order of the actual parameters matter when you have a required formal parameter mixed with some keywords? E.g. could I successfully call "(test-6 :foo 1 10 :bar 2)" and expect 10 to be bound to x?

RogerGjyieahTMOT
Автор

Thanks for this great tutorial, I am trying to follow your
instruction of the following code:
#+BEGIN_SRC elisp
(defun test-0 (x &optional (y 0))
(+ x y))
#+END_SRC
But I also get this:
#+BEGIN_SRC elisp
Debugger entered--Lisp error: (error "Malformed arglist: (x &optional (y 0))")
signal(error ("Malformed arglist: (x &optional (y 0))"))
error("Malformed arglist: %s" (x &optional (y 0)))
#f(compiled-function (name arglist &optional docstring decl &rest body) "Define NAME as a function.\nThe definition is (lambda ARGLIST [DOCSTRING] BODY...).\nSee also the function `interactive'.\nDECL is a declaration, optional, of the form (declare DECLS...) where\nDECLS is a list of elements of the form (PROP . VALUES). These are\ninterpreted according to return value is undefined." #<bytecode 0x100082173>)(test-0 (x &optional (y 0)) (+ x y))
macroexpand((defun test-0 (x &optional (y 0)) (+ x y)) nil)
macroexp-macroexpand((defun test-0 (x &optional (y 0)) (+ x y)) nil)
macroexp--expand-all((defun test-0 (x &optional (y 0)) (+ x y)))
macroexpand-all((defun test-0 (x &optional (y 0)) (+ x y)))
eval-sexp-add-defvars((defun test-0 (x &optional (y 0)) (+ x y)))
elisp--eval-last-sexp(nil)
eval-last-sexp(nil)
nil)
nil nil)

#+END_SRC

I think my code is the same as yours, not sure why this happen on
my machine: ~GNU Emacs 26.1 (build 1, x86_64-w64-mingw32) of
2018-05-30~

__-oewn
visit shbcf.ru