append - How can I delete a "." from a list in Common Lisp? - Stack Overflow

I'm new on lisp and I'm finding a difficulty while working with "append". I have to

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 with "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)

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 with "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)
Share Improve this question edited Nov 19, 2024 at 10:58 Will Ness 71.2k10 gold badges103 silver badges187 bronze badges asked Nov 18, 2024 at 19:45 M1ctl4nt3cutl1M1ctl4nt3cutl1 694 bronze badges 1
  • 1 The . means it isn't a "real" list, which should end with a nil. – Scott Hunter Commented Nov 18, 2024 at 19:50
Add a comment  | 

1 Answer 1

Reset to default 2

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 any other atom 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 element 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.


(*) nor is it NIL in which case it will get treated as the end of list marker, and disappear together with the dot. Which shows that NIL is an empty list, too:

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745597923a4635226.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信