Unquoted list problem

Started by cormullion, September 06, 2009, 03:13:35 PM

Previous topic - Next topic

cormullion

It always slightly puzzles me that you can have unquoted nested lists in lists. The manual is full of these:


(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:


(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?

Lutz

#1
;-> (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:


;-> (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.


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

cormullion

#2
Thanks! I've been away on holiday, and the brain cells aren't quite back to normal... :)