Lisp - Get rid of dotted list











up vote
0
down vote

favorite












I am building a function is Lisp ta reverses the first and last element of a list. I get that a list has a car and a cdr hence why there is a dot in my output. Is there a way to remove the dot?



(defun my-butlast (list)
(loop for l on list
while (cdr l)
collect (car l)))

(defun f-l-swap(list)
(append (last list)(cdr (my-butlast list))(car list))
)

(write(f-l-swap '(A B C D E)))

OUTPUT:
(E B C D . A)









share|improve this question




























    up vote
    0
    down vote

    favorite












    I am building a function is Lisp ta reverses the first and last element of a list. I get that a list has a car and a cdr hence why there is a dot in my output. Is there a way to remove the dot?



    (defun my-butlast (list)
    (loop for l on list
    while (cdr l)
    collect (car l)))

    (defun f-l-swap(list)
    (append (last list)(cdr (my-butlast list))(car list))
    )

    (write(f-l-swap '(A B C D E)))

    OUTPUT:
    (E B C D . A)









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am building a function is Lisp ta reverses the first and last element of a list. I get that a list has a car and a cdr hence why there is a dot in my output. Is there a way to remove the dot?



      (defun my-butlast (list)
      (loop for l on list
      while (cdr l)
      collect (car l)))

      (defun f-l-swap(list)
      (append (last list)(cdr (my-butlast list))(car list))
      )

      (write(f-l-swap '(A B C D E)))

      OUTPUT:
      (E B C D . A)









      share|improve this question















      I am building a function is Lisp ta reverses the first and last element of a list. I get that a list has a car and a cdr hence why there is a dot in my output. Is there a way to remove the dot?



      (defun my-butlast (list)
      (loop for l on list
      while (cdr l)
      collect (car l)))

      (defun f-l-swap(list)
      (append (last list)(cdr (my-butlast list))(car list))
      )

      (write(f-l-swap '(A B C D E)))

      OUTPUT:
      (E B C D . A)






      list common-lisp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 5 at 19:40









      rsm

      1,07631421




      1,07631421










      asked Nov 5 at 17:52









      mendy

      458




      458
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          append expects arguments to be lists. In your case (car list) is an atom. You have to change it to list if you want to stick with append. Ie:



          (defun f-l-swap (list)
          (append (last list)
          (cdr (my-butlast list))
          (list (car list))))





          share|improve this answer




























            up vote
            1
            down vote













            A list is a chain of cons pairs. eg. (1 2 3) is the visualization of (1 . (2 . (3 . ()))). In the event the last cdr is not () you have what we call a dotted list since then there is no simplified visualization of the last part. It has to be printed with a dot.



            You have (E . (B . (C . (D . A)))) and want to have (E . (B . (C . (D . (A . ()))))). Do you see the difference? (car list) is not a list but one elment and that is why you get a dotted list.



            Here are more sensible implementations of append and butlast:



            (defun my-append (a b)
            (if (null a)
            b
            (cons (car a) (my-append (cdr a) b))))


            That only supports 2 arguments, but the idea for more is that it continues until you have consed all the previous lists and only have one left, which verbatim becomes the tail. Here is how that might look:



            (defun my-append2 (x &rest xs)
            (labels ((helper (x xs)
            (cond ((null xs) x)
            ((null x) (helper (car xs) (cdr xs)))
            (t (cons (car x) (helper (cdr x) xs))))))
            (helper x xs)))


            Here is butlast



            (defun my-butlast (xs)
            (if (null (cdr xs))
            '()
            (cons (car xs) (my-butlast (cdr xs)))))


            Now, one should really do it with higher order functions or loop, but then you get the facts hidden how lists work. The code above shows you have they work.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159635%2flisp-get-rid-of-dotted-list%23new-answer', 'question_page');
              }
              );

              Post as a guest
































              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              append expects arguments to be lists. In your case (car list) is an atom. You have to change it to list if you want to stick with append. Ie:



              (defun f-l-swap (list)
              (append (last list)
              (cdr (my-butlast list))
              (list (car list))))





              share|improve this answer

























                up vote
                2
                down vote













                append expects arguments to be lists. In your case (car list) is an atom. You have to change it to list if you want to stick with append. Ie:



                (defun f-l-swap (list)
                (append (last list)
                (cdr (my-butlast list))
                (list (car list))))





                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  append expects arguments to be lists. In your case (car list) is an atom. You have to change it to list if you want to stick with append. Ie:



                  (defun f-l-swap (list)
                  (append (last list)
                  (cdr (my-butlast list))
                  (list (car list))))





                  share|improve this answer












                  append expects arguments to be lists. In your case (car list) is an atom. You have to change it to list if you want to stick with append. Ie:



                  (defun f-l-swap (list)
                  (append (last list)
                  (cdr (my-butlast list))
                  (list (car list))))






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 5 at 19:35









                  rsm

                  1,07631421




                  1,07631421
























                      up vote
                      1
                      down vote













                      A list is a chain of cons pairs. eg. (1 2 3) is the visualization of (1 . (2 . (3 . ()))). In the event the last cdr is not () you have what we call a dotted list since then there is no simplified visualization of the last part. It has to be printed with a dot.



                      You have (E . (B . (C . (D . A)))) and want to have (E . (B . (C . (D . (A . ()))))). Do you see the difference? (car list) is not a list but one elment and that is why you get a dotted list.



                      Here are more sensible implementations of append and butlast:



                      (defun my-append (a b)
                      (if (null a)
                      b
                      (cons (car a) (my-append (cdr a) b))))


                      That only supports 2 arguments, but the idea for more is that it continues until you have consed all the previous lists and only have one left, which verbatim becomes the tail. Here is how that might look:



                      (defun my-append2 (x &rest xs)
                      (labels ((helper (x xs)
                      (cond ((null xs) x)
                      ((null x) (helper (car xs) (cdr xs)))
                      (t (cons (car x) (helper (cdr x) xs))))))
                      (helper x xs)))


                      Here is butlast



                      (defun my-butlast (xs)
                      (if (null (cdr xs))
                      '()
                      (cons (car xs) (my-butlast (cdr xs)))))


                      Now, one should really do it with higher order functions or loop, but then you get the facts hidden how lists work. The code above shows you have they work.






                      share|improve this answer

























                        up vote
                        1
                        down vote













                        A list is a chain of cons pairs. eg. (1 2 3) is the visualization of (1 . (2 . (3 . ()))). In the event the last cdr is not () you have what we call a dotted list since then there is no simplified visualization of the last part. It has to be printed with a dot.



                        You have (E . (B . (C . (D . A)))) and want to have (E . (B . (C . (D . (A . ()))))). Do you see the difference? (car list) is not a list but one elment and that is why you get a dotted list.



                        Here are more sensible implementations of append and butlast:



                        (defun my-append (a b)
                        (if (null a)
                        b
                        (cons (car a) (my-append (cdr a) b))))


                        That only supports 2 arguments, but the idea for more is that it continues until you have consed all the previous lists and only have one left, which verbatim becomes the tail. Here is how that might look:



                        (defun my-append2 (x &rest xs)
                        (labels ((helper (x xs)
                        (cond ((null xs) x)
                        ((null x) (helper (car xs) (cdr xs)))
                        (t (cons (car x) (helper (cdr x) xs))))))
                        (helper x xs)))


                        Here is butlast



                        (defun my-butlast (xs)
                        (if (null (cdr xs))
                        '()
                        (cons (car xs) (my-butlast (cdr xs)))))


                        Now, one should really do it with higher order functions or loop, but then you get the facts hidden how lists work. The code above shows you have they work.






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          A list is a chain of cons pairs. eg. (1 2 3) is the visualization of (1 . (2 . (3 . ()))). In the event the last cdr is not () you have what we call a dotted list since then there is no simplified visualization of the last part. It has to be printed with a dot.



                          You have (E . (B . (C . (D . A)))) and want to have (E . (B . (C . (D . (A . ()))))). Do you see the difference? (car list) is not a list but one elment and that is why you get a dotted list.



                          Here are more sensible implementations of append and butlast:



                          (defun my-append (a b)
                          (if (null a)
                          b
                          (cons (car a) (my-append (cdr a) b))))


                          That only supports 2 arguments, but the idea for more is that it continues until you have consed all the previous lists and only have one left, which verbatim becomes the tail. Here is how that might look:



                          (defun my-append2 (x &rest xs)
                          (labels ((helper (x xs)
                          (cond ((null xs) x)
                          ((null x) (helper (car xs) (cdr xs)))
                          (t (cons (car x) (helper (cdr x) xs))))))
                          (helper x xs)))


                          Here is butlast



                          (defun my-butlast (xs)
                          (if (null (cdr xs))
                          '()
                          (cons (car xs) (my-butlast (cdr xs)))))


                          Now, one should really do it with higher order functions or loop, but then you get the facts hidden how lists work. The code above shows you have they work.






                          share|improve this answer












                          A list is a chain of cons pairs. eg. (1 2 3) is the visualization of (1 . (2 . (3 . ()))). In the event the last cdr is not () you have what we call a dotted list since then there is no simplified visualization of the last part. It has to be printed with a dot.



                          You have (E . (B . (C . (D . A)))) and want to have (E . (B . (C . (D . (A . ()))))). Do you see the difference? (car list) is not a list but one elment and that is why you get a dotted list.



                          Here are more sensible implementations of append and butlast:



                          (defun my-append (a b)
                          (if (null a)
                          b
                          (cons (car a) (my-append (cdr a) b))))


                          That only supports 2 arguments, but the idea for more is that it continues until you have consed all the previous lists and only have one left, which verbatim becomes the tail. Here is how that might look:



                          (defun my-append2 (x &rest xs)
                          (labels ((helper (x xs)
                          (cond ((null xs) x)
                          ((null x) (helper (car xs) (cdr xs)))
                          (t (cons (car x) (helper (cdr x) xs))))))
                          (helper x xs)))


                          Here is butlast



                          (defun my-butlast (xs)
                          (if (null (cdr xs))
                          '()
                          (cons (car xs) (my-butlast (cdr xs)))))


                          Now, one should really do it with higher order functions or loop, but then you get the facts hidden how lists work. The code above shows you have they work.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 5 at 22:19









                          Sylwester

                          33.4k22853




                          33.4k22853






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159635%2flisp-get-rid-of-dotted-list%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest




















































































                              這個網誌中的熱門文章

                              Academy of Television Arts & Sciences

                              L'Équipe

                              1995 France bombings