Unquoted list problem

Q&A's, tips, howto's
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Unquoted list problem

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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)

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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

Locked