libera/#lisp - IRC Chatlog
Search
19:25:55
ori
hi, I'm making my first steps with lisp, and I'm a bit confused by why/when quoting functions is required. For example, here: (apply #'+ '(1 2 3)) -- why do we need to prevent '+' from being evaluated?
19:27:31
dash[m]
in common lisp `#'` doesn't actually quote, it retrieves the function value for the symbol
19:27:33
jackdaniel
if you had written (apply + '(1 2 3)) then apply would try to look up the function bound to +
19:28:35
jackdaniel
yes, 'xxx is a shortened version of (quote xxx) and as dash[m] points out #'xxx is a shortened version of (function xxx)
19:30:18
jackdaniel
there are lisps with shared namespace for both variables and functions, most notably scheme
19:33:43
ori
ok, that makes sense, I think. But here's another example I'm confused about (these are from ch. 2 of Paul Graham's ANSI Common LISP): (funcall #'(lambda (x) (+ x 100))
19:34:15
pjb
(values (let ((+ (function *))) (apply + '(1 2 3))) (let ((+ (function +))) (apply + '(1 2 3)))) #| --> 6 ; 6 |#
19:34:49
pjb
You cannot just bang onto the machine, you need to know what you're doing banging on it.
19:35:01
jackdaniel
and it isn't necessary (it was planned to be necessary, but for compatibility with islisp lambda has been defined as a macro that expands to a function)
19:36:02
pjb
dash[m]: #' creates the closure. #'x expands to (function x) and it is function that creates the closures.
19:38:00
jackdaniel
I don't think that this would mislead anyone, unless we are teaching elementary arithmetics
19:38:26
pjb
ori: #'x = (function x) 'x = (quote x) (flet ((x () 33)) (let ((x 42)) (list x (quote x) (x) (function x)))) #| --> (42 x 33 #<Compiled-function x (Non-Global) #x30200263B98F>) |#
19:39:34
pjb
ori: x can name a function (here a local function defined with flet), a variable (here a local variable defined with let). In an expression, we may want to get the result of a call to the function; this is done with the parentheses (x) ; or we may want the value bound to the variable; this is done with just the variable name: x
19:40:05
pjb
ori: or we may want to get the symbol naming the variable itself, then we need to quote it: (quote x) returns X instead of the value of X.
19:40:18
blueyoner
people dont write useful software in lisp, they just engage in casuistical arguments about trivia
19:40:38
pjb
ori: and for functions, we may want to get the function itself, instead of its name, So we quote it with (function x), because functions and variables are in different namespaces.
19:40:53
jackdaniel
blueyoner: and you extrapolate these insightful conclusions from the irc channel? of course people /chat/ on irc channel
19:41:53
pjb
ori: note that you can define your own namespaces, and use the symbol x to name other things than variables or functions.
19:42:54
pjb
ori: for example, packages are named with strings, and package operator take string designators, so you can use X as a designator to name a package: (defpackage x (:use cl)) (in-package x) ; <--- X names a package here.
19:43:47
pjb
ori: (actually, it's the name of the symbol X, namely the string "X" which names the package)
19:43:51
sham1
Well, the actual and sad reason for the relative unpopular-ness is the s-expressions. It's such a non-issue but it turns people off quite a bit. Especially since not many editors support structured editing
19:44:52
pjb
ori: you could define operators, such as (define-color …) and (color x) and (mix (color x) (color blue0)) and here, x would name a color. (list (quote x) (color x) …))
19:44:57
ori
I can't say I understand everything but the overall vibe I'm getting is that these are particulars of the language that I'll pick up over time (I hope) rather than a Big Concept I'm failing to grok.
19:45:14
pjb
let's be happy we don't have to write (variable x) to get the value of x (sometimes we use (symbol-value x) however).
19:46:27
pjb
sham1: well, it's not that horrible, it's quite nice to make it explicit, eg. in a formal semantics of the language.