I've written this simple buildPairs
function which is supposed to take a list and "something" and return a list of pairs, where each pair consists of one element in the list and the "something" argument.
4(defun buildPairs(aList rightChild)
5 "returns a list of pairs"
6 (setq ret (list (cons (car aList)
7 rightChild)))
8 (setq newList (cdr aList))
9 (while newList
10 (print (car newList))
11 (setq ret (append ret
12 (list(cons (car newList)
13 rightChild))))
14
15 (setq newList (cdr newList)))
16
17 ret)
18
19(setq listA '("a1", "a2", "a3"))
20
21
22(length (buildPairs listA "foo"))
I would expect line 22
to returns 3.
But for some reason, it return 5, and I HAVE NO CLUE why it's doing that.
The list that it returns is (("a1" . "foo") (\, . "foo") ("a2" . "foo") (\, . "foo") ("a3" . "foo"))
, which is VERY odd.
Could any one please explain what I did wrong?
I get a different result when I run your code:
(("a1" . "foo") ((\, "a2") . "foo") ((\, "a3") . "foo"))
This happens because the value of listA
is actually:
("a1" (\, "a2") (\, "a3"))
You probably meant to write this line without commas:
(setq listA '("a1" "a2" "a3"))
The comma has a special meaning in backquote syntax. As it applies a special meaning to the following list element, it seems like the reader renders it as a function call, i.e. a list that contains the "function" name and the arguments.