I'm using this plist as a datastructure:
SHALA-SYS> (pass-of (student-from-name "mozart reina"))
(:TYPE M :START-DATE #2015-01-03T15:29:25.000000+09:00 :AMT 17000)
And using this table as a reference to match the types with certain numerical values:
(defparameter *type-map* '((M . 30)
(E . 30)
(W . 7)))
So using assoc in the REPL works as expected:
SHALA-SYS> (assoc (getf (pass-of (student-from-name "mozart reina"))
:type)
*type-map*)
(M . 30)
But when I run the exact same code in Hunchentoot I get nil instead:
(define-easy-handler (dummy-fn :uri "/dummy-fn") ()
(standard-page (:title "")
(htm
(fmt "~A" (assoc (getf (pass-of (student-from-name "mozart reina"))
:type)
*type-map*)))))
NIL
Has anyone had this experience? The only thing I can think of is that somehow MongoDB, which I'm using to persist the data, is somehow screwing with the symbols since it saves them as strings but I run intern on them to get turn them back into symbols, and the REPL doesn't have a problem with it.
This is a FAQ: Using symbols as keys does not seem to work.
If one wants to use symbols as keys into data structures like property-lists, assoc-lists, hash-tables, CLOS objects, ... then one has to make sure that the symbols are interned in the correct package. cl:*package* is the current package and this variable may have different values. For example during development and runtime of a program the value of the default package may be different.
When using CL:INTERN it is useful to give the package as an argument or to bind the cl:*package* variable.
CL-USER 10 > (intern "FOO")
FOO
NIL
CL-USER 11 > (symbol-package *)
#<The COMMON-LISP-USER package, 155/256 internal, 0/4 external>
CL-USER 12 > (INTERN "FOO" "HTTP-USER")
HTTP-USER::FOO
NIL
CL-USER 13 > (let ((*package* (find-package "HTTP-USER")))
(intern "FOO"))
HTTP-USER::FOO
:INTERNAL
Related
In Common Lisp, the special operator quote makes whatever followed by un-evaluated, like
(quote a) -> a
(quote {}) -> {}
But why the form (quote ()) gives me nil? I'm using SBCL 1.2.6 and this is what I got in REPL:
CL-USER> (quote ())
NIL
More about this problem: This is some code from PCL Chapter 24
(defun as-keyword (sym)
(intern (string sym) :keyword))
(defun slot->defclass-slot (spec)
(let ((name (first spec)))
`(,name :initarg ,(as-keyword name) :accessor ,name)))
(defmacro define-binary-class (name slots)
`(defclass ,name ()
,(mapcar #'slot->defclass-slot slots)))
When the macro expand for the following code:
(define-binary-class id3-tag
((major-version)))
is
(DEFCLASS ID3-TAG NIL
((MAJOR-VERSION :INITARG :MAJOR-VERSION :ACCESSOR MAJOR-VERSION)))
which is NIL rather than () after the class name ID3-TAG.
nil and () are two ways to express the same concept (the empty list).
Traditionally, nil is used to emphasize the boolean value "false" rather than the empty list, and () is used the other way around.
The Common LISP HyperSpec says:
() ['nil], n. an alternative notation for writing the symbol nil, used
to emphasize the use of nil as an empty list.
Your observation is due to an object to having more than one representation. In Common Lisp the reader (that reads code and reads expressions) parses text to structure and data. When it's data the writer can print it out again but it won't know exactly how the data was represented when it was initially read in. The writer will print one object exactly one way, following defaults and settings, even though there are several representations for that object.
As you noticed nil, NIL, nIL, NiL, ... ,'nil, 'NIL, (), and '() are all read as the very same object. I'm not sure the standard dictates exactly how it's default representation out should be so I guess some implementations choose one of NIL, nil or maybe even ().
With cons the representation is dependent on the cdr being a cons/nil or not:
'(a . nil) ; ==> (a)
'(a . (b . c)) ; ==> (a b . c)
'(a . (b . nil)) ; ==> (a b)
With numbers the reader can get hints about which base you are using. If no base is used in the text it will use whatever *read-base* is:
(let ((*read-base* 2)) ; read numbers as boolean
(read-from-string "(10 #x10)")) ; ==> (2 16)
#x tells the reader to interpret the rest as a hexadecimal value. Now if your print-base would have been 4 the answer to the above would have been visualized as (2 100).
To sum it up.. A single value in Common Lisp may have several good representations and all of them would yield the very same value. How the value is printed will follow both implementation, settings and even arguments to the functions that produce them. Neither what it accepts as values in or the different ways it can visualize the value tells nothing about how the value actually gets stored internally.
Problem
Given an instance, inst and a string attr containing the name of a slot, how can I obtain the value of the slot attr on inst?
Of course, if attr were a symbol rather than a string, I would typically just use (slot-value inst attr), but it seems I need the package information to properly call intern (see below).
Minimal example
(defpackage :pack1
(:use :common-lisp)
(:export :*inst*))
(in-package :pack1)
(defclass temp-class ()
((temp-slot :initarg :temp-slot)))
(defvar *inst* (make-instance 'temp-class :temp-slot "value"))
(defpackage :pack2
(:use :common-lisp :pack1)
(:import-from :pack1 :temp-class))
(in-package :pack2)
(let ((inst *inst*) ; In the real example, inst gets defined outside my control,
; in yet another package
(attr "temp-slot"))
(format t "Given package name: ~S; " ; prints fine
(slot-value inst (intern (string-upcase attr) :pack1)))
(format t "No package name: ~S; " ; signals an error
(slot-value inst (intern (string-upcase attr)))))
Prior art
From this question, I figured out that my problem was that intern was creating symbols in a different package than the one in which the class was defined.
It seems from this question, that I can't extract the package information simply from the instance, so I'll have to figure out another way (besides using intern to get there)
Background
I'm working on py-format a Common Lisp port of
Python's {}-formatting. To implement the Python . operator (getattr) I need to convert the
string following the dot into a slot on the object preceding the dot.
Given an instance, inst and a string attr containing the name of a slot, how can I obtain the value of the slot attr on inst?
Slots don't have strings as slots names, but symbols. Since slot names can be arbitrary symbols, there is no general way to get a slot-value if all you have is a string.
CL-USER 124 > (defclass foo ()
((s) ; the slot-name is cl-user::s
(system::s) ; the slot-name is system::s
(#:s))) ; the slot-name is #:s
#<STANDARD-CLASS FOO 413054236B>
The last slot-name is an uninterned symbol. It is in no package.
Thus you can't look it up in any way, if you haven't stored it somewhere.
CL-USER 125 > (make-instance 'foo)
#<FOO 402013F043>
CL-USER 126 > (describe *)
#<FOO 402013F043> is a FOO
S #<unbound slot>
S #<unbound slot>
S #<unbound slot>
As you see above, it has three slots. Each symbol has the name s, but is really a different symbol.
You can get the slot names via introspection:
CL-USER 127 > (mapcar #'slot-definition-name
(class-direct-slots (find-class 'foo)))
(S SYSTEM::S #:S)
For portable functions see CLOSER-MOP.
What is the difference between the keyword symbol
:foo
and the quoted symbol:
'foo
Both stand for themselves, and can be used as an identifier. I can see that keyword symbols are mainly used for named parameters, but I was asking myself if it was not possible to implement this using quoted symbols as well?
In other words: Why do I need both?
First: 'something is a shorter notation for (quote something). The reader will transform the quote character into a list with the symbol cl:quote as the first item. For the evaluator it means: don't evaluate something, just return it as a result.
CL-USER 22 > '(quote foo)
(QUOTE FOO)
CL-USER 23 > ''foo
(QUOTE FOO)
CL-USER 24 > (read-from-string "'foo")
(QUOTE FOO)
The colon : is a package marker. If the package name is missing, the symbol is in the KEYWORD package.
We can give foo a value:
CL-USER 11 > (setq foo 10)
10
foo evaluates to its value.
CL-USER 12 > foo
10
A quoted symbol evaluates to the symbol.
CL-USER 13 > 'foo
FOO
We can't give :foo a value:
CL-USER 14 > (setq :foo 10)
Error: Cannot setq :FOO -- it is a keyword.
1 (abort) Return to level 0.
2 Return to top loop level 0.
Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.
CL-USER 15 : 1 > :top
:foo already has a value: itself.
CL-USER 16 > :foo
:FOO
Naturally a quoted :foo evaluates to :foo.
CL-USER 17 > ':foo
:FOO
The symbol foo is in some package, here CL-USER.
CL-USER 18 > (symbol-package 'foo)
#<The COMMON-LISP-USER package, 92/256 internal, 0/4 external>
The symbol :foo is in the KEYWORD package.
CL-USER 19 > (symbol-package ':foo)
#<The KEYWORD package, 0/4 internal, 6230/8192 external>
Since :foo is the value of :foo we can also write:
CL-USER 20 > (symbol-package :foo)
#<The KEYWORD package, 0/4 internal, 6230/8192 external>
:foo is an abbreviation for keyword:foo. Thus the symbol is in the keyword package and it is exported.
CL-USER 21 > keyword:foo
:FOO
So keyword symbols are self-evaluation constant symbols in the keyword package. They are used as markers in data structures and in keyword arglists. The good things: you don't need to struggle with packages and they evaluate to themselves - so a quote is not needed.
DIfferences between keywords and other symbols
Rainer Joswig's answer describes the symbols themselves pretty well. To summarize, though, each symbol belongs to a package. p::foo (or p:foo, if it's external) is a symbol in the package p. If you try to evaluate it as form, you'll get its symbol-value, which you can set with set, or `(setf symbol-value):
CL-USER> (set 'foo 'bar)
BAR
CL-USER> foo
BAR
CL-USER> (setf (symbol-value 'foo) 'baz)
BAZ
CL-USER> foo
BAZ
There's a special package named keyword. You can write keyword::foo if you want, but all of the keyword package's symbol are external, so you can write keyword:foo instead. Because they're so commonly used, though, you even get a special syntax for them: :foo. They've also got the special property that you can't set their value; their values are themselves:
CL-USER> :foo
:FOO
CL-USER> (symbol-value :bar)
:BAR
And that's really all there is that makes keyword symbols special, in and of themselves.
Keywords and other symbols as keyword names in lambda lists
What's probably a bit more important is that they are, by default, used as indicators for "keyword arguments" in lambda lists. E.g.,
CL-USER> ((lambda (&key foo bar)
(list foo bar))
:bar 23 :foo 12)
(12 23)
I can see that keyword symbols are mainly used for named parameters,
but I was asking myself if it was not possible to implement this using
quoted symbols as well?
The syntax for lambda lists actually lets you do a lot more customization with the keyword arguments. A common thing is to specify default values:
CL-USER> ((lambda (&key (foo 'default-foo) bar)
(list foo bar))
:bar 23)
(DEFAULT-FOO 23)
You can also provide a variable name that gets bound to a boolean indicating whether the parameter was specified or not:
CL-USER> ((lambda (&key (foo 'default-foo foo-p) (bar 'default-bar bar-p))
(format t "~{~A:~7t~A~%~}"
(list 'foo foo
'foo-p foo-p
'bar bar
'bar-p bar-p)))
:bar 23)
FOO: DEFAULT-FOO
FOO-P: NIL
BAR: 23
BAR-P: T
The full syntax for from 3.4.1 Ordinary Lambda Lists lets us do even more, though. It's
lambda-list::= (var*
[&optional {var | (var [init-form [supplied-p-parameter]])}*]
[&rest var]
[&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
[&aux {var | (var [init-form])}*])
Note that you can specify the keyword-name. It defaults to the symbol in the keyword package with the same name as var, but you can actually provide it and specify your own "keywords". This can be handy, e.g., if you want a descriptive keyword name but don't want such a long variable name:
CL-USER> ((lambda (&key ((:home-directory dir)))
(list dir))
:home-directory "/home/me")
("/home/me")
You can also use it to specify keyword names that aren't keyword symbols:
CL-USER> ((lambda (&key ((surprise surprise)))
(list surprise))
'surprise "hello world!")
("hello world!")
You can combine the two, too:
CL-USER> ((lambda (&key ((hidden-parameter secret)))
(format t "the secret is ~A" secret))
'hidden-parameter 42)
the secret is 42
You can mix that with default values as well, but you probably get the point by now.
Can someone please give an example of how to write a row to a table using a variable as the table name in Lisp/Postmodern/S-SQL/PostgreSQL?
I am currently able to write to a table in PostgreSQL with :insert-into using the following function:
(defun save-event (table-name cost event-id)
(defprepared insert-event
(sql-compile `(:insert-into ,table-name :set 'cost ,cost
'event-id ,event-id)))
(insert-event))
Using the input syntax:
(save-event 'table-name 33 1)
However, if I try and pass a string containing the desired table name to the function as follows:
(defparameter x "table-name")
(apply #'funcall `(save-event ',x 44 2))
I get the following error message:
Database error 42601: syntax error at or near "E'table-name'"
Query: INSERT INTO E'table-name' (cost, event_id) VALUES (44, 2)
[Condition of type CL-POSTGRES-ERROR:SYNTAX-ERROR-OR-ACCESS-VIOLATION]
I have been stuck on this for quite a while, tried almost everything. Got me stumped.
If the code expects a symbol as table name you cannot pass a string, you must intern it.
Something like the following should work...
(defparameter x "table-name")
(save-event (intern x) 44 2)
In common lisp a symbol is a symbol and a string is a string. To get a string from a symbol you need to call (symbol-name x) to get a symbol from a string you need to call (intern x).
Quasiquoting (back quoting) is not going to do this transformation. In other words:
(let ((test "foo"))
`(this is a ,test))
--> (THIS IS A "foo")`
Depending on how the symbol is going to be used by that library may be even an "uninterned" symbol can be used instead of a regular interned symbol.
Those symbols can be created with (make-symbol x) but if this is acceptable or not depends on how the symbol is being used in the code; note also that while (intern x) will always return the same symbol if you call it multiple times with the same string this is not true for (make-symbol x) that instead will return a different fresh uninterned symbol each time is called.
(make-symbol "X")
--> #:X
(symbol-name (make-symbol "X"))
--> "X"
(eq (make-symbol "X") (make-symbol "X"))
--> NIL
(eq (intern "X") (intern "X"))
--> T
Given a list such as
(list "foo" "bar" nil "moo" "bar" "moo" nil "affe")
how would I build a new list with the duplicate strings removed, as well as the nils stripped, i.e.
(list "foo" "bar" "moo" "affe")
The order of the elements needs to be preserved - the first occurence of a string may not be removed.
The lists I'm dealing with here are short, so there's no need to use anything like a hash table for the uniqueness check, although doing so certainly wouldn't hurt either. However, using cl functionality is not a viable option.
Try "Sets and Lists" in the "Lists" section of the Emacs Lisp Reference Manual:
(delq nil (delete-dups (list "foo" "bar" nil "moo" "bar" "moo" nil "affe")))
The Common Lisp package contains many list manipulation functions, in particular remove-duplicates.
(require 'cl)
(remove-duplicates (list "foo" "bar" nil "moo" "bar" "moo" nil "affe")
:test (lambda (x y) (or (null y) (equal x y)))
:from-end t)
Yes, I realize you said you didn't want to use cl. But I'm still mentioning this as the right way to do it for other people who might read this thread.
(Why is cl not viable for you anyway? It's been shipped with Emacs for about 20 years now, not counting less featured past incarnations.)
If you use dash.el library, that's all you need:
(-distinct (-non-nil '(1 1 nil 2 2 nil 3)) ; => (1 2 3)
dash.el is written by Magnar Sveen and it's a great list manipulation library with many functions for all kinds of tasks. I recommend to install it if you write lots of Elisp code. Function -distinct removes duplicate elements in a list, -non-nil removes nil elements. While the above code is sufficient, below I describe an alternative approache, so feel free to ignore the rest of the post.
-non-nil was added in version 2.9, so if for some reason you have to use earlier versions, another way to achieve the same is to use -keep with built-in identity function, which just returns whatever it is given: (identity 1) ; => 1. The idea is that -keep keeps only elements, for which the predicate returns true (“non-nil” in Lisp jargon). identity obviously returns non-nil only for whatever values that are not nil:
(-distinct (-keep 'identity '(1 1 nil 2 2 nil 3)) ; => (1 2 3)
This is a short example:
(delete-duplicates '("~/.emacs.d" "~/.emacs.d") :test #'string-equal) ;; '("~/emacs.d")
Basically you use the :test keyword to select the function string-equal to test if the elements are duplicated.
Else the default function test doesn't check string equality.
Here ya go:
(defun strip-duplicates (list)
(let ((new-list nil))
(while list
(when (and (car list) (not (member (car list) new-list)))
(setq new-list (cons (car list) new-list)))
(setq list (cdr list)))
(nreverse new-list)))