What would be the easiest way to convert '(a b c) to ("a" "b" "c")? I'd like to use the string-join function but it only takes in a list of strings.
(map symbol->string '(a b c)) as pointed out by #alexis-king.
Related
I need to be able to compare two cars of a list to sort them im LISP.
Lists '(e d) (a b)
I want to compare the cars (e and a). This works using eql. If they don't match, I want to order the lists alphabetically, so (a b) (e d).
I'm missing the part where I can see which character is 'bigger', so the check if e or a should come first. I've tried converting them to ascii codes, but that doesn't work for (car a). Using arithmetic operators such as '<' and '>' also doesn't work. Does anyone have an idea on how to do this?
Use string> without symbol-name:
CL-USER 6 > (string> 'a 'b)
NIL
CL-USER 7 > (string< 'a 'b)
0
For the sake of completeness, here is how you should use it inside sort to achieve desired result (sort is destructive- modifies used sequence, so I also used copy-tree to avoid that effect):
(let ((data '((e d) (a b))))
(sort (copy-tree data)
(lambda (x y) (string< (car x) (car y)))))
((A B) (E D))
A symbol is distinct from a string.
CL-USER> (symbol-name 'foo)
"FOO"
A string (a sequence of characters) can be compared in the manner you seem to be interested in.
CL-USER> (string> "FOO" "BOO")
0
CL-USER> (string< "FOO" "BOO")
NIL
I want to paste between every element of a lista special element. In example:
(EINFUEGEN '(A B C) '*);-> (A * B * C)
How can I implement that on the easiest way?
The fun way:
(cdr (mapcan #'list '#1=(* . #1#) '(a b c)))
The respectable way:
(loop
for (x . xs) on '(a b c)
collect x
when xs collect '*)
for <var> on <list> iterates over all sublists, meaning var will be bound to (a b c), then (b c) then (c) and finally ().
(x . xs) is a destructuring notation to bind respectively x and xs to the head and tail of each list being visited. This is necessary here to check whether there are remaining elements.
collect <val> adds <val> to implicit collection being built
when <test> <clause> executes the LOOP clause <clause> only when <test> is satisfied. Here, I test if there are more elements in the list; when it is the case, I also collect the star symbol.
I want to apply the union function to the lists within a list. For example:
apply union to the lists inside this list: '((a b c) (a d))
Is there a function that "unwraps" a list, to reveal the sequence of elements inside the list? For example:
unwrapping this list '((a b c) (a d)) produces this sequence '(a b c) '(a d)
If I could do that, then I could apply the union function to the sequence.
What is the recommended idiom for taking the union of a sequence of lists contained within a list?
CL-USER 15 > (reduce #'union '((a b c) (a d)))
(D A B C)
I want to give a number and return the element of this position.
List lab = (R K K K K) and I want to know if something like this (position 1 lab) exists on lisp. Like in C return lab[1].
In Common Lisp the operator that gets the n-th element of a list is called nth (see the manual):
(nth 2 '(a b c d)) ; returns C
A related operator is nthcdr that returns the rest of the list starting from the n-th element:
(nthcdr 2 '(a b c d)) ; returns (C D)
For an operator that works on vectors and proper lists, see elt.
(let ((list (list 'a 'b 'c 'd)))
(prog1 list
(setf (elt list 1) 1)))
=> (A 1 C D)
I want to define a function that consumes 2 lists and do subtraction resulting another list.
For example, when list1 is '(a a b b c) and list2 is '(a b), the subtraction result should be '(a b c).
I tried to implement it by lambda and remove, ended up making sevral lists.
I really have no idea how to do this.
Here is one way:
#lang racket
(define (subtract xs ys)
(if (empty? ys)
xs
(subtract (remove (first ys) xs) (rest ys))))
(subtract '(a a b b c) '(a b))