Concatenate two lists in Common Lisp
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
lisp common-lisp
edited 2 days ago
Rainer Joswig
109k8163280
109k8163280
asked 2 days ago
lawrence313
175
175
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
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)
seems so obvious now... thanks!
– lawrence313
2 days ago
add a comment |
up vote
2
down vote
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)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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)
seems so obvious now... thanks!
– lawrence313
2 days ago
add a comment |
up vote
3
down vote
accepted
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)
seems so obvious now... thanks!
– lawrence313
2 days ago
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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)
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)
answered 2 days ago
Rainer Joswig
109k8163280
109k8163280
seems so obvious now... thanks!
– lawrence313
2 days ago
add a comment |
seems so obvious now... thanks!
– lawrence313
2 days ago
seems so obvious now... thanks!
– lawrence313
2 days ago
seems so obvious now... thanks!
– lawrence313
2 days ago
add a comment |
up vote
2
down vote
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)
add a comment |
up vote
2
down vote
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)
add a comment |
up vote
2
down vote
up vote
2
down vote
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)
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)
edited yesterday
answered yesterday
Renzo
16.2k42742
16.2k42742
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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