Page 1 of 1

Unquoted list problem

Posted: Sun Sep 06, 2009 10:13 pm
by cormullion
It always slightly puzzles me that you can have unquoted nested lists in lists. The manual is full of these:

Code: Select all

(set 'data '((monday (apples 20 30) (oranges 2 4 9)) (tuesday (apples 5) (oranges 32 1))))
Usually that's fine - I'll probably not evaluate oranges as a function.

But here's a problem I have at the moment:

Code: Select all

(set 'f '(find "a" l))
(set 'c "abc")
(set-ref-all 'l f c)
(println f)
;-> (find "a" "abc")
(println (eval f))
;-> 0

; so it's ok if c is not a list

(set 'f '(find "a" l))
(set 'c '("a" "b" "c"))   ; it's quoted
(set-ref-all 'l f c)
(println f)
;-> (find "a" ("a" "b" "c"))     ; no quote now.... :(
(println (eval f))
;-> ERR: value expected in function find : "b"

so even when I thought I'd inserted a quoted list, I hadn't, and the error was inevitable. Is there a nice way to write this to work for any type of c?

Posted: Mon Sep 07, 2009 12:00 am
by Lutz

Code: Select all

;-> (find "a" ("a" "b" "c"))     ; no quote now.... :(
(println (eval f))
;-> ERR: value expected in function find : "b" 
'find' is trying to evaluate its second argument. Because the first element in ("a" "b" "c") is a string it expects an index number but gets "b".

Remember that user-defined functions (and most built-ins) evaluate their args first.

In your first example:

Code: Select all

;-> (find "a" "abc")
(println (eval f))
;-> 0 
the same thing happens, but the string "abc" just evaluates to itself (it is not a list like in the second example).

If you quote the 'c', it will work for both cases of 'c' either being a string or a list.

Code: Select all

(set-ref-all 'l f 'c)

Posted: Mon Sep 07, 2009 9:32 pm
by cormullion
Thanks! I've been away on holiday, and the brain cells aren't quite back to normal... :)