I need to build a list like:
(list (list 1 2) (list 3 4) (list 5 6)), but there is an error in the following code:
(define example
(lambda (a b)
(let ((tmp (+ b 1)))
(list '(1 b) '(2 tmp) '(3 3)))))
(example 1 4)
The result that I want is (list (list 1 4) (list 3 5) (list 3 3)) but the result I get: (list (list 1 'b) (list 2 'tmp) (list 3 3)).
You need to do
(list (list 1 b) (list 2 tmp) '(3 3))
or
(list `(1 ,b) `(2 ,tmp) '(3 3))
Related
I will keep this short and simple.
Want: To edit elements in a nested list. This nested list will have an unlimited number of elements but the list inside the list will always have 2 elements, like (list (list 1 2) (list 3 4)). I wanted to add a specific value 'x' to the first element of each nested list using recursion. e.g., (change (list (list 1 2) (list 10 20) 3), where 3 is the 'x' value => (list (list 4 2) (list 13 23))
Code:
(define (change lst x)
(cond
[(empty? lst) empty]
[else (cons (+ (first (first lst)) x)
(cons (second (first lst)) (change (rest lst) x)))]))
Output: an input such as (change (list (list 12 2) (list 1 2)) 100) will produce (list 112 2 101 2).
Problem: The x value is added, but it should produce (list (list 112 2) (list 101 2)). Itt is not nested.
try this:
(define +n
(lambda (n)
(lambda (l)
(fold-right
(lambda (x a)
(cons (+ (car x) n)
(cons (cadr x)
a)))
'()
l ))))
(define 100+ (+n 100))
(100+ (list (list 12 2) (list 1 2)))
So in lisp a list is a collection of cons nodes, each node has two parts to it. The car of the node and the cdr of that node, how would I go about reversing each cons node?
Using reduce:
(defun reverse-conses (list)
(reduce (lambda (x acc) (cons acc x)) list :initial-value nil :from-end t))
Recursively:
(defun reverse-conses (list)
(if (null list) nil
(cons (reverse-conses (cdr list)) (car list))))
I'm starting with a single function that swaps a cons cell.
(defun swap-cons (cns)
(cons (cdr cns)
(car cns)))
Let's test it:
> (swap-cons (cons 1 2))
(2 . 1)
> (swap-cons (cons 1 (cons 2 3)))
((2 . 3) . 1)
So this works. Now we just need to map this function over the input list
(defun swap-conses (lst)
(mapcar #'swap-cons
lst))
> (swap-conses '((1 . 2)))
((2 . 1))
> (swap-conses '((1 . 2) (3 . 4)))
((2 . 1) (4 . 3))
> (swap-conses '((1 2)))
(((2) . 1))
> (swap-conses '((1 . 2) (3 4) (5 6 7)))
((2 . 1) ((4) . 3) ((6 7) . 5))
To recursively go through a whole tree and swap car and cdr you can do something like this:
(defun reverse-conses (tree)
(if (consp tree)
(cons (reverse-conses (cdr tree))
(reverse-conses (car tree)))
tree))
(reverse-conses (cons 1 2)) ; ==> (2 . 1)
(reverse-conses '(1 2 3)) ; ==> (((nil . 3) . 2) . 1)
(reverse-conses '(1 (2 3) 4)) ; ==> (((nil . 4) (nil . 3) . 2) . 1)
Considering the argument can consist of improper lists there isn't a simpler solution to this.
Why the result of (cons (list 1 2) (list 3 4)) is ((1 2) 3 4)?
I wonder why the result length is 3(3 elements). My intuition makes me think that
(list 1 2) is a list, (list 3 4) is also a list. By using cons procedure, result should be two elements with each element a list, but the result is not as I expect.
Can anybody tell me why? Thanks.
(list a b c) is equivalent to (cons a (list b c)) by definition (or, if you continue the transformation, (cons a (cons b (cons c nil))).
So if you write (cons 1 (list 3 4)), this is equivalent to (cons 1 (cons 3 (cons 4 nil))), or to (list 1 3 4).
Now, replace 1 with (list 1 2), and you get this: (cons (list 1 2) (cons 3 (cons 4 nil))), or equivalently (list (list 1 2) 3 4) (or, fully written out, (cons (cons 1 (cons 2 nil)) (cons 3 (cons 4 nil)))).
The key here is that cons is not append, nor list (which treat all their elements equally): it is inherently asymetrical, when dealing with lists. The left spot holds the element ("head"); the right one holds the rest of the list ("tail").
I'm trying to take a list of lists of lists (don't worry, I'll put an example) and convert the elements of each last least into one.
This is what I've done so far:
(defun cost(state)
(let ((list_elements '()))
(dolist (element state)
(dolist (subElement element)
(setq list_elements (append list_elements (list subElement))))
finally (return list_elements))))
Example:
(list
(list
(list
(list 1 9 't121)
(list 1 10 't122))
(list
(list 2 10 't123)
(list 2 11 't124)))
(list
(list
(list 1 9 't121)
(list 1 11 't132))
(list
(list 2 11 't133)
(list 2 12 't134))))
So, this is supposed to return
((1 9 T121) (1 10 T122) (2 10 T123) (2 11 T124) (1 9 T121) (1 11 T132) (1 11 T132) (2 11 T133) (2 12 T134))
And it is only returning ((1 9 T121) (1 11 T132))
After that, I'm supposed to count the number of different elements in the list.
Does anyone see what the problem is in this function?
(defun double-append (list)
(reduce #'append (reduce #'append list)))
;; or like this:
(defun mapcan-mapcon (list)
(mapcan #'append (mapcon #'car list)))
(double-append (list
(list
(list
(list 1 9 't121)
(list 1 10 't122))
(list
(list 2 10 't123)
(list 2 11 't124)))
(list
(list
(list 1 9 't121)
(list 1 11 't132))
(list
(list 2 11 't133)
(list 2 12 't134)))))
((1 9 T121) (1 10 T122) (2 10 T123) (2 11 T124) (1 9 T121) (1 11 T132)
(2 11 T133) (2 12 T134))
So far I could tell by the expected result, that must be something like it.
;; Using Alexandria, just as an example, of how currying can save
;; some repetitive coding:
(ql:quickload "alexandria")
(defun curried-append ()
(let ((reducer (alexandria:curry #'reduce #'append)))
(alexandria:compose reducer reducer)))
(funcall
(curried-append)
(list
(list
(list
(list 1 9 't121)
(list 1 10 't122))
(list
(list 2 10 't123)
(list 2 11 't124)))
(list
(list
(list 1 9 't121)
(list 1 11 't132))
(list
(list 2 11 't133)
(list 2 12 't134)))))
If you have a list of list of lists, how do you access the rest of the first of the list?
e.g. If you have
(define l1 (list (list (list 1 1) (list 2 3) (list 7 8))
(list (list 2) (list 3 4 5))))
How would you perform a recursion on this part
(list (list 1 1) (list 2 3) (list 7 8))
When I try (rest (first l1))
e.g. (map add1 (rest (first l1))
I get an error add1: expects a number; given (list 2 3)
You're exactly right that the code
(rest (first l1))
produces the rest of the first of the list. Specifically:
(define l1 (list (list (list 1 1) (list 2 3) (list 7 8))
(list (list 2) (list 3 4 5))))
(rest (first l1))
produces
(list (list 2 3) (list 7 8))
It's true that you can't add the elements of this list together, because they're not numbers.
Does this answer your question?