Concatenate two lists in Common Lisp












2















I set myself to the task to write a Common Lisp function that concatenates two lists without using append.



Common Lisp input (concat-lists '(1 2 3) '(4 5 6)) should return (1 2 3 4 5 6)



Even though my solution seems to work it looks overtly complicated



(defun concat-lists(seq1 seq2)
(cond ((not (null seq1)) (cons (car seq1) (concat-lists (cdr seq1) seq2)))
(T (cond ((not (null seq2)) (cons (car seq2) (concat-lists seq1 (cdr seq2))))
(T nil)))))


What I'm looking for is a more elegant solution using reduce where I use seq1 as initial value and then apply a function to each element of seq2, thereby appending each value of the list to seq1. Somehow I always get stuck when trying....



Any help or input is much appreciated. Thanks!










share|improve this question





























    2















    I set myself to the task to write a Common Lisp function that concatenates two lists without using append.



    Common Lisp input (concat-lists '(1 2 3) '(4 5 6)) should return (1 2 3 4 5 6)



    Even though my solution seems to work it looks overtly complicated



    (defun concat-lists(seq1 seq2)
    (cond ((not (null seq1)) (cons (car seq1) (concat-lists (cdr seq1) seq2)))
    (T (cond ((not (null seq2)) (cons (car seq2) (concat-lists seq1 (cdr seq2))))
    (T nil)))))


    What I'm looking for is a more elegant solution using reduce where I use seq1 as initial value and then apply a function to each element of seq2, thereby appending each value of the list to seq1. Somehow I always get stuck when trying....



    Any help or input is much appreciated. Thanks!










    share|improve this question



























      2












      2








      2








      I set myself to the task to write a Common Lisp function that concatenates two lists without using append.



      Common Lisp input (concat-lists '(1 2 3) '(4 5 6)) should return (1 2 3 4 5 6)



      Even though my solution seems to work it looks overtly complicated



      (defun concat-lists(seq1 seq2)
      (cond ((not (null seq1)) (cons (car seq1) (concat-lists (cdr seq1) seq2)))
      (T (cond ((not (null seq2)) (cons (car seq2) (concat-lists seq1 (cdr seq2))))
      (T nil)))))


      What I'm looking for is a more elegant solution using reduce where I use seq1 as initial value and then apply a function to each element of seq2, thereby appending each value of the list to seq1. Somehow I always get stuck when trying....



      Any help or input is much appreciated. Thanks!










      share|improve this question
















      I set myself to the task to write a Common Lisp function that concatenates two lists without using append.



      Common Lisp input (concat-lists '(1 2 3) '(4 5 6)) should return (1 2 3 4 5 6)



      Even though my solution seems to work it looks overtly complicated



      (defun concat-lists(seq1 seq2)
      (cond ((not (null seq1)) (cons (car seq1) (concat-lists (cdr seq1) seq2)))
      (T (cond ((not (null seq2)) (cons (car seq2) (concat-lists seq1 (cdr seq2))))
      (T nil)))))


      What I'm looking for is a more elegant solution using reduce where I use seq1 as initial value and then apply a function to each element of seq2, thereby appending each value of the list to seq1. Somehow I always get stuck when trying....



      Any help or input is much appreciated. Thanks!







      lisp common-lisp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 23:39









      Rainer Joswig

      111k8169286




      111k8169286










      asked Nov 20 '18 at 22:51









      lawrence313lawrence313

      177




      177
























          3 Answers
          3






          active

          oldest

          votes


















          4














          CL-USER 39 > (reduce #'cons
          '(1 2 3 4 5)
          :initial-value '(a b c d e)
          :from-end t)
          (1 2 3 4 5 A B C D E)





          share|improve this answer
























          • seems so obvious now... thanks!

            – lawrence313
            Nov 21 '18 at 0:15



















          4














          The solution of Rainer Joswig is really elegant and simple, and respects your request of using reduce.



          If you want to see also a recursive, simple solution, then here is the classical one:



          (defun concat-lists (seq1 seq2)
          (if (null seq1)
          seq2
          (cons (car seq1) (concat-lists (cdr seq1) seq2))))

          (concat-lists '(1 2 3) '(4 5 6))
          (1 2 3 4 5 6)





          share|improve this answer

































            2














            I do understand your requirement for 'reduce'. and here other options:



            CL also has 'concatenante'



            (concatenate 'list '(1 2 3) '(4 5 6))



            There is also the other less complicated (IMHO), and not as elegant.



            (defun concat-lists (list1 list2)
            (let ((a (copy-list list1))
            (b (copy-list list2)))
            (rplacd (last a) b)
            a))



            or



            (defun concat-lists (list1 list2)
            (let ((a (copy-list list1))
            (b (copy-list list2)))
            (nconc a b)))






            share|improve this answer





















            • 1





              There is no need to copy list2 in both your functions, since it will not be modified.

              – Renzo
              Nov 25 '18 at 10:27











            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',
            autoActivateHeartbeat: false,
            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%2f53402780%2fconcatenate-two-lists-in-common-lisp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            CL-USER 39 > (reduce #'cons
            '(1 2 3 4 5)
            :initial-value '(a b c d e)
            :from-end t)
            (1 2 3 4 5 A B C D E)





            share|improve this answer
























            • seems so obvious now... thanks!

              – lawrence313
              Nov 21 '18 at 0:15
















            4














            CL-USER 39 > (reduce #'cons
            '(1 2 3 4 5)
            :initial-value '(a b c d e)
            :from-end t)
            (1 2 3 4 5 A B C D E)





            share|improve this answer
























            • seems so obvious now... thanks!

              – lawrence313
              Nov 21 '18 at 0:15














            4












            4








            4







            CL-USER 39 > (reduce #'cons
            '(1 2 3 4 5)
            :initial-value '(a b c d e)
            :from-end t)
            (1 2 3 4 5 A B C D E)





            share|improve this answer













            CL-USER 39 > (reduce #'cons
            '(1 2 3 4 5)
            :initial-value '(a b c d e)
            :from-end t)
            (1 2 3 4 5 A B C D E)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 23:46









            Rainer JoswigRainer Joswig

            111k8169286




            111k8169286













            • seems so obvious now... thanks!

              – lawrence313
              Nov 21 '18 at 0:15



















            • seems so obvious now... thanks!

              – lawrence313
              Nov 21 '18 at 0:15

















            seems so obvious now... thanks!

            – lawrence313
            Nov 21 '18 at 0:15





            seems so obvious now... thanks!

            – lawrence313
            Nov 21 '18 at 0:15













            4














            The solution of Rainer Joswig is really elegant and simple, and respects your request of using reduce.



            If you want to see also a recursive, simple solution, then here is the classical one:



            (defun concat-lists (seq1 seq2)
            (if (null seq1)
            seq2
            (cons (car seq1) (concat-lists (cdr seq1) seq2))))

            (concat-lists '(1 2 3) '(4 5 6))
            (1 2 3 4 5 6)





            share|improve this answer






























              4














              The solution of Rainer Joswig is really elegant and simple, and respects your request of using reduce.



              If you want to see also a recursive, simple solution, then here is the classical one:



              (defun concat-lists (seq1 seq2)
              (if (null seq1)
              seq2
              (cons (car seq1) (concat-lists (cdr seq1) seq2))))

              (concat-lists '(1 2 3) '(4 5 6))
              (1 2 3 4 5 6)





              share|improve this answer




























                4












                4








                4







                The solution of Rainer Joswig is really elegant and simple, and respects your request of using reduce.



                If you want to see also a recursive, simple solution, then here is the classical one:



                (defun concat-lists (seq1 seq2)
                (if (null seq1)
                seq2
                (cons (car seq1) (concat-lists (cdr seq1) seq2))))

                (concat-lists '(1 2 3) '(4 5 6))
                (1 2 3 4 5 6)





                share|improve this answer















                The solution of Rainer Joswig is really elegant and simple, and respects your request of using reduce.



                If you want to see also a recursive, simple solution, then here is the classical one:



                (defun concat-lists (seq1 seq2)
                (if (null seq1)
                seq2
                (cons (car seq1) (concat-lists (cdr seq1) seq2))))

                (concat-lists '(1 2 3) '(4 5 6))
                (1 2 3 4 5 6)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 '18 at 15:04

























                answered Nov 21 '18 at 10:25









                RenzoRenzo

                18k43044




                18k43044























                    2














                    I do understand your requirement for 'reduce'. and here other options:



                    CL also has 'concatenante'



                    (concatenate 'list '(1 2 3) '(4 5 6))



                    There is also the other less complicated (IMHO), and not as elegant.



                    (defun concat-lists (list1 list2)
                    (let ((a (copy-list list1))
                    (b (copy-list list2)))
                    (rplacd (last a) b)
                    a))



                    or



                    (defun concat-lists (list1 list2)
                    (let ((a (copy-list list1))
                    (b (copy-list list2)))
                    (nconc a b)))






                    share|improve this answer





















                    • 1





                      There is no need to copy list2 in both your functions, since it will not be modified.

                      – Renzo
                      Nov 25 '18 at 10:27
















                    2














                    I do understand your requirement for 'reduce'. and here other options:



                    CL also has 'concatenante'



                    (concatenate 'list '(1 2 3) '(4 5 6))



                    There is also the other less complicated (IMHO), and not as elegant.



                    (defun concat-lists (list1 list2)
                    (let ((a (copy-list list1))
                    (b (copy-list list2)))
                    (rplacd (last a) b)
                    a))



                    or



                    (defun concat-lists (list1 list2)
                    (let ((a (copy-list list1))
                    (b (copy-list list2)))
                    (nconc a b)))






                    share|improve this answer





















                    • 1





                      There is no need to copy list2 in both your functions, since it will not be modified.

                      – Renzo
                      Nov 25 '18 at 10:27














                    2












                    2








                    2







                    I do understand your requirement for 'reduce'. and here other options:



                    CL also has 'concatenante'



                    (concatenate 'list '(1 2 3) '(4 5 6))



                    There is also the other less complicated (IMHO), and not as elegant.



                    (defun concat-lists (list1 list2)
                    (let ((a (copy-list list1))
                    (b (copy-list list2)))
                    (rplacd (last a) b)
                    a))



                    or



                    (defun concat-lists (list1 list2)
                    (let ((a (copy-list list1))
                    (b (copy-list list2)))
                    (nconc a b)))






                    share|improve this answer















                    I do understand your requirement for 'reduce'. and here other options:



                    CL also has 'concatenante'



                    (concatenate 'list '(1 2 3) '(4 5 6))



                    There is also the other less complicated (IMHO), and not as elegant.



                    (defun concat-lists (list1 list2)
                    (let ((a (copy-list list1))
                    (b (copy-list list2)))
                    (rplacd (last a) b)
                    a))



                    or



                    (defun concat-lists (list1 list2)
                    (let ((a (copy-list list1))
                    (b (copy-list list2)))
                    (nconc a b)))







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 24 '18 at 14:21

























                    answered Nov 23 '18 at 22:42









                    Ala'a MohammadAla'a Mohammad

                    8124




                    8124








                    • 1





                      There is no need to copy list2 in both your functions, since it will not be modified.

                      – Renzo
                      Nov 25 '18 at 10:27














                    • 1





                      There is no need to copy list2 in both your functions, since it will not be modified.

                      – Renzo
                      Nov 25 '18 at 10:27








                    1




                    1





                    There is no need to copy list2 in both your functions, since it will not be modified.

                    – Renzo
                    Nov 25 '18 at 10:27





                    There is no need to copy list2 in both your functions, since it will not be modified.

                    – Renzo
                    Nov 25 '18 at 10:27


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402780%2fconcatenate-two-lists-in-common-lisp%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)