admin管理员组文章数量:1023187
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 Answer
Reset to default 2At 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 )
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 anil
. – Scott Hunter Commented Nov 18, 2024 at 19:50
1 Answer
Reset to default 2At 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 )
本文标签: appendHow can I delete a quotquot from a list in Common LispStack Overflow
版权声明:本文标题:append - How can I delete a "." from a list in Common Lisp? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745597922a2158281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.
means it isn't a "real" list, which should end with anil
. – Scott Hunter Commented Nov 18, 2024 at 19:50