If you have copied correctly your code in the post, you have used two different names for the same function: my_attach
(_ is the underscore character) and my-attach
(- is the dash character).
Simply use the same name. For instance:
(defun my-attach(O L)
(cond ((eq L nil ) O)
(t (cons (car L) (my-attach O (cdr L))))))
But note that this will not produce the desired result (in fact it does not produce a proper list). Rather, you should use (list O)
instead of O
in the terminal case:
CL-USER> (defun my-attach(O L)
(cond ((eq L nil) O)
(t (cons (car L) (my-attach O (cdr L))))))
MY-ATTACH
CL-USER> (my-attach 3 '(1 2))
(1 2 . 3)
CL-USER> (my-attach 3 '())
3
CL-USER> (defun my-attach(O L)
(cond ((eq L nil) (list O))
(t (cons (car L) (my-attach O (cdr L))))))
MY-ATTACH
CL-USER> (my-attach 3 '(1 2))
(1 2 3)
CL-USER> (my-attach 3 '())
(3)
Finally, a note about the common writing conventions for Common Lisp programs:
it is preferred to use lower case identifiers;
compound words in identifiers are separated by -
, not by _
as in other languages;
it is preferred to use if
when there are only two cases in a conditional statement;
check for a value which is nil
is commonly done with the predicate null
;
the parentheses at the end of an expression are usually written on the same line of the last part of the expression.
So your function could be rewritten as:
(defun my-attach (o l)
(if (null l)
(list o)
(cons (car l) (my-attach o (cdr l)))))