listappendlispcommon-lisp

How can I delete a "." from a list on common lisp?


I'm new on lisp and I'm finding a difficulty while working with "append". I have to reorder a list and put the first element of the input list as the last of the output list. I've trying whit "append" and "nconc", and any time I get the list I want, but with a "." before the last element. What this dot means? Is there any way to avoid this symbol to appear?

Thanks a lot!

 (nconc (rest l)(first l)) >> (B C D E . A)

 (append (rest l) (first l)) >> (B C D E . A)

Solution

  • At the most basic level, lists are created with a series of cons calls:

    (cons 1       2     )  ; (1 . 2)
    (cons 1 (cons 2 NIL))  ; (1 2 . NIL)  = (1 2)
    
    (cons 1 (cons 2       3     ))  ; (1 2 . 3)
    (cons 1 (cons 2 (cons 3 NIL)))  ; (1 2 3 . NIL) = (1 2 3) 
    

    NIL is the special marker to signal the end of a list. Hence, the NIL after the . can just disappear together with the .. But with anything else after the ., it can't.

    append is a higher-level function, which appends two lists:

    ; l = (1 2 3)
    (append (rest l) (first l))
    =
    (cons 2 (cons 3   1))
    =
    (     2       3 . 1)
    

    This is so because first l here is not a list. But we can make a list out of it:

    ; l = (1 2 3)
    (append (rest l) (list (first l)))
    =
    (cons 2 (cons 3  (list 1)))
    =
    (cons 2 (cons 3  (cons 1   NIL)))
    =
    (     2       3        1 . NIL  )
    =
    (     2       3        1        )
    

    nconc in this respect is just like append.