I'm trying to learning Common Lisp reading Ansi Common Lisp from Paul Graham and Using EEC325 course critique and run-tests functions and the lectures. I set up Emacs with slime and SBCL
The problem is on chapter 3 exercise 8 says:
Define a function that takes a list and prints it in dot notation:
> (showdots ' ( a b c)) (A . (B . (C . NIL))) NIL
I made the following function that the result is a string and it works well for the cases but doesn't print which is the main objective of the exercise
(defun show-dots (lst)
(cond
((atom lst) (format nil "~A" lst ))
((consp lst) (format nil "(~A . ~A)"
(show-dots (car lst))
(show-dots (cdr lst))))))
The problem is that it produces the string not prints the String but it works
CS325-USER> (SHOW-DOTS '(A B C))
"(A . (B . (C . NIL)))"
CS325-USER> (SHOW-DOTS '(A (B C)))
"(A . ((B . (C . NIL)) . NIL))"
CS325-USER> (SHOW-DOTS '(A . B))
"(A . B)"
CS325-USER> (SHOW-DOTS NIL)
"NIL"
CS325-USER> (SHOW-DOTS '(NIL))
"(NIL . NIL)"
The test expects for printing so really it fails but it is obvious the problems comes when printing the result
(run-tests show-dots)
SHOW-DOTS: (SHOW-DOTS '(A B C)) failed:
Should have printed "(A . (B . (C . NIL)))" but saw ""
SHOW-DOTS: (SHOW-DOTS '(A (B C))) failed:
Should have printed "(A . ((B . (C . NIL)) . NIL))" but saw ""
SHOW-DOTS: (SHOW-DOTS '(A . B)) failed:
Should have printed "(A . B)" but saw ""
SHOW-DOTS: (SHOW-DOTS NIL) failed:
Should have printed "NIL" but saw ""
SHOW-DOTS: (SHOW-DOTS '(NIL)) failed:
Should have printed "(NIL . NIL)" but saw ""
SHOW-DOTS: 0 assertions passed, 5 failed.
so i thought the only thing that I must do is print this string, after creating but this things doesnt' working and I do not understand why
1) attemp
(defun show-dots (lst)
(cond
((atom lst) (format t "~A" lst ))
((consp lst) (format t "(~A . ~A)"
(show-dots (car lst))
(show-dots (cdr lst))))))
with this results
CS325-USER> (SHOW-DOTS '(A B C))
ABCNIL(NIL . NIL)(NIL . NIL)(NIL . NIL)
NIL
CS325-USER> (SHOW-DOTS '(A (B C)))
ABCNIL(NIL . NIL)(NIL . NIL)NIL(NIL . NIL)(NIL . NIL)
NIL
CS325-USER> (SHOW-DOTS '(A . B))
AB(NIL . NIL)
NIL
CS325-USER> (SHOW-DOTS NIL)
NIL
NIL
CS325-USER> (SHOW-DOTS '(NIL))
NILNIL(NIL . NIL)
NIL
so I said Ok, this is crazy first string and the print it
(defun show-dots (lst)
(format t (cond
((atom lst) (format nil "~A" lst ))
((consp lst) (format nil "(~A . ~A)"
(show-dots (car lst))
(show-dots (cdr lst)))))))
but the result is not the correct
CS325-USER> (SHOW-DOTS '(A B C))
ABCNIL(NIL . NIL)(NIL . NIL)(NIL . NIL)
NIL
CS325-USER> (SHOW-DOTS '(A (B C)))
ABCNIL(NIL . NIL)(NIL . NIL)NIL(NIL . NIL)(NIL . NIL)
NIL
CS325-USER> (SHOW-DOTS '(A . B))
AB(NIL . NIL)
NIL
CS325-USER> (SHOW-DOTS NIL)
NIL
NIL
CS325-USER> (SHOW-DOTS '(NIL))
NILNIL(NIL . NIL)
NIL
So I said ok let's create a local variable put there the string and print it, but it doesn't work again
(defun show-dots (lst)
(let ((str (cond
((atom lst) (format nil "~A" lst ))
((consp lst) (format nil "(~A . ~A)"
(show-dots (car lst))
(show-dots (cdr lst)))))))
(format t str)))
and the result is not correct
CS325-USER> (SHOW-DOTS '(A B C))
ABCNIL(NIL . NIL)(NIL . NIL)(NIL . NIL)
NIL
CS325-USER> (SHOW-DOTS '(A (B C)))
ABCNIL(NIL . NIL)(NIL . NIL)NIL(NIL . NIL)(NIL . NIL)
NIL
CS325-USER> (SHOW-DOTS '(A . B))
AB(NIL . NIL)
NIL
CS325-USER> (SHOW-DOTS NIL)
NIL
NIL
CS325-USER> (SHOW-DOTS '(NIL))
NILNIL(NIL . NIL)
NIL
So I really want to understand what happens here, maybe is something silly but I do not get the point.
Thanks for yout time