I'm working with lists in Racket, and I was doing a function to additions items.
I did
(define (iter lst [acc '()])
(if (null? lst)
(append (list acc))
(iter10 (cdr lst) (append acc (caar lst)))))
and i have output:
(iter10 '(((1 2)(3 4)(5 6))((7 8)(9 10)(11 12))))
'((1 2 7 8))
but i want output:
(iter10 '(((1 2)(3 4)(5 6))((7 8)(9 10)(11 12))))
'((1 2 7 8) (3 4 9 10) (5 6 11 12))
(iter10 '(((1 2)(3 4)(5 6))((7 8)(9 10)(11 12))((1 2)(3 4)(5 6))))
'((1 2 7 8 1 2) (3 4 9 10 3 4) (5 6 11 12 5 6))
I know it's because of the use of caar, but I don't know how to do it any other way.
There is a main list in which there are several list. I want all elements to be connected by position. And that the number of main list could be any. Thanks
Break this problem into two parts:
Use apply map idiom to create groups of lists with same position:
> (apply map list '(((1 2)(3 4)(5 6))((7 8)(9 10)(11 12))))
'(((1 2) (7 8)) ((3 4) (9 10)) ((5 6) (11 12)))
Use append to append all lists in group into one list:
> (map (lambda (group) (apply append group))
'(((1 2) (7 8)) ((3 4) (9 10)) ((5 6) (11 12))))
'((1 2 7 8) (3 4 9 10) (5 6 11 12))
Then combine these two parts into one function:
(define (iter10 lst)
(map (lambda (group) (apply append group))
(apply map list lst)))
Tests:
> (iter10 '(((1 2)(3 4)(5 6))((7 8)(9 10)(11 12))))
'((1 2 7 8) (3 4 9 10) (5 6 11 12))
> (iter10 '(((1 2)(3 4)(5 6))((7 8)(9 10)(11 12))((1 2)(3 4)(5 6))))
'((1 2 7 8 1 2) (3 4 9 10 3 4) (5 6 11 12 5 6))
Related
i.e. I am given this (all possible combinations to make change for 11):
(list 1 1 1 1 1 1 1 5 5 5 5 1 1 1 10 10 10 1 1 25 25 25 25 25 25)
My code should return:
((7 . 1) (4 . 5) (3 . 1) (3 . 10) (2 . 1) (6 . 25))
so it is more readable.
However, it is returning:
((7 . 1) (4 . 5) (1 . 3) (3 . 10) (2 . 1) (6 . 25))
I don't know how to reverse the order of ALL my pairs.
This is my code:
(define (rev pair) ;; function reverses a pair, not a list.
(map (lambda (e) (cons (cdr e) (car e))) pair))
(define (rle coins)
(if (null? coins)
'()
(let loop ((firsts (list (car coins)))
(but-firsts (cdr coins)))
(if (or (null? but-firsts)
(not (equal? (car firsts) (car but-firsts))))
(rev (cons (cons (car firsts) (length firsts))
(rle but-firsts)))
(rev (loop (cons (car but-firsts) firsts) (cdr but-firsts)))))))
This is my test:
(rle (list 1 1 1 1 1 1 1 5 5 5 5 1 1 1 10 10 10 1 1 25 25 25 25 25 25))
Why not writing a classical tail-call recursive function?
Because my opinion is that named-loops are horrible constructs - unclear for the reader.
(define (rle coins (acc '()))
(if (null? coins)
(reverse acc)
(rle (cdr coins)
(if (and (not (null? acc)) (equal? (cdar acc) (car coins)))
(cons (cons (+ 1 (caar acc))
(cdar acc))
(cdr acc))
(cons (cons 1 (car coins))
acc)))))
It does correctly:
(rle (list 1 1 1 1 1 1 1 5 5 5 5 1 1 1 10 10 10 1 1 25 25 25 25 25 25))
;; '((7 . 1) (4 . 5) (3 . 1) (3 . 10) (2 . 1) (6 . 25))
First, some systematic testing (test cases should be systematic and as small as possible, not large and arbitrary):
> (rle '(2))
'((1 . 2))
> (rle '(2 3))
'((1 . 2) (3 . 1))
> (rle '(2 3 4))
'((1 . 2) (3 . 1) (1 . 4))
> (rle '(2 3 4 5))
'((1 . 2) (3 . 1) (1 . 4) (5 . 1))
> (rle '(2 3 4 5 6))
'((1 . 2) (3 . 1) (1 . 4) (5 . 1) (1 . 6))
When all the elements are unique, it looks like every other element is backwards.
> (rle '(5 5 6))
'((5 . 2) (1 . 6))
> (rle '(5 5 6 6))
'((5 . 2) (6 . 2))
> (rle '(5 5 6 6 6))
'((5 . 2) (3 . 6))
> (rle '(5 6 6 6))
'((1 . 5) (6 . 3))
When elements are repeated, the order seems a bit unpredictable.
Let's count how many times you rev each element by using triple elements with the counter as the car:
;; Flip the order of a pair.
(define (flip x)
(cons (cdr x) (car x)))
;; Add one to the car of each element and flip its cdr.
(define (rev list)
(map (lambda (x) (cons (+ 1 (car x)) (flip (cdr x)))) list))
(define (rle coins)
(if (null? coins)
'()
(let loop ((firsts (list (car coins)))
(but-firsts (cdr coins)))
(if (or (null? but-firsts)
(not (equal? (car firsts) (car but-firsts))))
(rev (cons (cons 0 (cons (car firsts) (length firsts)))
(rle but-firsts)))
(rev (loop (cons (car but-firsts) firsts) (cdr but-firsts)))))))
This creates a list that says how many times rev has been applied to each element in the result.
> (rle '(99))
'((1 1 . 99))
> (rle '(99 99))
'((2 99 . 2))
> (rle '(99 99 99))
'((3 3 . 99))
> (rle '(99 99 99 100))
'((3 3 . 99) (4 100 . 1))
> (rle '(99 99 99 100 100))
'((3 3 . 99) (5 2 . 100))
As you can see, each element has been "flipped" many times - once for every time you recurse.
This happens because you apply rev to every partial result, and not just on the final list.
The elements that have been flipped an even number of times are "backwards".
Now, your example input:
> (rle (list 1 1 1 1 1 1 1 5 5 5 5 1 1 1 10 10 10 1 1 25 25 25 25 25 25))
'((7 7 . 1) (11 4 . 5) (14 1 . 3) (17 3 . 10) (19 2 . 1) (25 6 . 25))
where we can see that the third element has been flipped an even number of times, and this is what's causing it to be backwards.
The best solution is to build your pairs in the order you want them, so you don't need to adjust them afterwards; the implementation is left as an exercise.
(The implementation is a very minor change in your procedure, and the removal of rev.)
how do i append (1 2 3) to the end of () to make ((1 2 3))
how do i append (4 5 6) to the end of that to make ((1 2 3) (4 5 6))
how do i append "|" to the end of that to make ((1 2 3) (4 5 6) "|")
with NO dotted pairs.
I'm working with Chicken Scheme but I'll take an answer from any scheme at this point. Note that any of these lists could also be nested lists of who knows what... i'm just writing a trivial example.
note: #sjamaan shows a solution using append that involves wrapping everything in another list to compensate for append doing things OTHER than what the name says.
(append (list 1 2 3) "|" ) ;=> (1 2 3 . "|")
;^^ didn't actually append, created a dotted pair
(append '(1 2 3) (list 4 5 6)) ;=> (1 2 3 4 5 6) ; don't want unwrapped list
;^^ didn't actually append the list i gave it but appended the contents of the list.
Basically I'm hoping for an append method that actually appends what you give it, not appends the contents of it, or takes it and makes a dotted pair. Maybe i'm just a dreamer... I can write a "no really append" method that just takes whatever params you give it and wraps them in an outer list to compensate but that's just silly... Surely scheme has some way to append without this crazyness.
Here is how append is made:
(define (append2 lst1 lst2)
(if (null? lst1)
lst2 ; the second list is unaltered
(cons (car lst1)
(append2 (cdr lst1) lst2))))
makes a pair chain consisting of all the elements in lst1 and lst2. It does not make a pair where there is nont in lst2 so:
(append2 '(1 2 3) '(4 5)) ; ==> (1 2 3 4 5)
(append2 '(1 2 3) '()) ; ==> (1 2 3) and not (1 2 3 ())
(append2 '(1 2 3) '5) ; ==> (1 2 3 . 5)
Note that every list like (1 2 3) actually is (1 2 3 . ()) or even more correctly (1 . (2 . (3 . ())))
how do i append (1 2 3) to the end of () to make ((1 2 3))
(define (insert-last e lst)
(let helper ((lst lst))
(if (pair? lst)
(cons (car lst)
(helper (cdr lst)))
(cons e '()))))
(insert-last '(1 2 3) '())
; ==> ((1 2 3))
how do i append (4 5 6) to the end of that to make ((1 2 3) (4 5
6))
(insert-last '(4 5 6) '((1 2 3)))
; ==> ((1 2 3) (4 5 6))
how do i append "|" to the end of that to make ((1 2 3) (4 5 6)
"|")
(insert-last "|" '((1 2 3) (4 5 6)))
; ==> ((1 2 3) (4 5 6) "|")
Know that this is very much like append. These are the worst way to make that list since you are making a new list every time. It's O(n) for each insert and O(n^2) for n elements. If you could do this in reverse order you get something that do this O(1) instead of O(n) for each insert. Instead of insert-last you use cons:
(cons '"|" '()) ; ==> ("|")
(cons '(4 5 6) '("|")) ; ==> ((4 5 6) "|")
(cons '(1 2 3) '((4 5 6) "|") ; ==> ((1 2 3) (4 5 6) "|")
This is O(1), O(n) for n elements processed. If you need to do it in the original order you can accumulate, then reverse..
(cons '(1 2 3) '()) ; ==> ((1 2 3))
(cons '(4 5 6) '((1 2 3))) ; ==> ((4 5 6) (1 2 3))
(cons '"|" '((4 5 6) (1 2 3))) ; ==> ("|" (4 5 6) (1 2 3))
(reverse '("|" (4 5 6) (1 2 3)) ; ==> ((1 2 3) (4 5 6) "|")
This is O(1), then O(n) for the reverse but it still is O(1) amortized. O(n) for n elements you process.
append doesn't append atoms to lists. It concatenates lists. You have to lift the atom up to a list before concatenation makes sense.
(append xs (list y))
But it makes sense to point out (reverse (cons y (reverse xs))) which has the same result. reverse suggests that you might be building up your list backwards if you need to append atoms to the end.
The procedure you're looking for is unsurprisingly called append (from SRFI-1). It appends a list of things onto another list. This does mean that if you want to add just one item, you'll need to make a list out of it:
(append '() '((1 2 3))) => ((1 2 3))
(append '((1 2 3)) '((4 5 6))) => ((1 2 3) (4 5 6))
(append '((1 2 3) (4 5 6)) '("|") ) => ((1 2 3) (4 5 6) "|")
It accepts multiple arguments, which will all be appended to eachother in that order, so you can also do:
(append '() '((1 2 3)) '((4 5 6)) '("|")) => ((1 2 3) (4 5 6) "|")
Hope this helps!
Whether you want it or not, cons cells will be created, since lists consist of cons cells.
how do i append (1 2 3) to the end of () to make ((1 2 3))
CL-USER 24 > (list '(1 2 3))
((1 2 3))
how do i append (4 5 6) to the end of that to make ((1 2 3) (4 5 6))
CL-USER 25 > (append '((1 2 3)) (list '(4 5 6)))
((1 2 3) (4 5 6))
how do i append "|" to the end of that to make ((1 2 3) (4 5 6) "|")
CL-USER 26 > (append '((1 2 3) (4 5 6)) (list "|"))
((1 2 3) (4 5 6) "|")
I'm trying to print pairs from one list kinda like a subset in scheme but with two elements just like this
(1 2 3 4 5)
((1 2) (1 3) (1 4) (1 5) (2 3) (2 4) (2 5) (3 4) (3 5) (4 5))
the code I wrote doesn't work
(define (subset x)
(if ( null? x) x
(map cons x (subset (cdr x)))))
this just return an empty list
I prefer to write the lambdas explicitly, makes it easier to understand what arguments are passed:
(define subset
(lambda (lst)
(if (null? lst)
lst
(append (map (lambda (el) (cons (car lst) el)) (cdr lst))
(subset (cdr lst)))
)))
(subset '(1 2 3 4 5))
=> ((1 . 2) (1 . 3) (1 . 4) (1 . 5) (2 . 3) (2 . 4) (2 . 5) (3 . 4) (3 . 5) (4 . 5))
EDIT: The explanation about map below is only valid in some versions of scheme, read Sylwester's comment to this answer.
map traverses n lists supplied to it and applies proc to the n elements in the same position in the lists. This means it can apply proc no more times than the length of the shortest list, but you keep giving it an empty list (from the last recursive call backwards).
(BTW this is in plain scheme)
In #lang racket that is very easy since we have combinations:
(combinations '(1 2 3 4 5) 2)
; ==> ((1 2) (1 3) (2 3) (1 4) (2 4) (3 4) (1 5) (2 5) (3 5) (4 5))
Now this does not print anything. To get it print to the terminal you can use displayln:
(displayln (combinations '(1 2 3 4 5) 2))
; ==> #<void>, ((1 2) (1 3) (2 3) (1 4) (2 4) (3 4) (1 5) (2 5) (3 5) (4 5)) printed to terminal as side effect
If order of items is also important, following can be used:
(define (subsets l)
(let loop ((n 0) ; run a loop for each item
(ol '())) ; start with blank output list
(cond
[(= n (length l)) (reverse ol)] ; output ol when last item reached;
[else
(let* ((x (list-ref l n)) ; get one item
(jl (for/list ((i l) ; get remaining list
(j (in-naturals))
#:unless (= j n))
i))
(kl (for/list ((i jl)) ; make combinations with each of remaining list
(list x i))))
(loop (add1 n) (cons kl ol)))])))
Testing:
(subsets '(1 2 3 4 5))
Output:
'(((1 2) (1 3) (1 4) (1 5))
((2 1) (2 3) (2 4) (2 5))
((3 1) (3 2) (3 4) (3 5))
((4 1) (4 2) (4 3) (4 5))
((5 1) (5 2) (5 3) (5 4)))
I have 2 dynamic lists that I would like to merge into one.
Say
'(1 2 3 4)
and
'(15 16)
and get
'(1 2 3 4 15 16)
How can this be done?
Use append for this:
(append '(1 2 3 4) '(15 16))
=> '(1 2 3 4 15 16)
Or, if you're merging two sorted lists and need to maintain the ordering in the result list:
(require srfi/1)
(define (merge-sorted-lists lhs rhs #:order (lt? <))
(let loop ((result '())
(lhs lhs)
(rhs rhs))
(cond ((null? lhs) (append-reverse result rhs))
((null? rhs) (append-reverse result lhs))
((lt? (car rhs) (car lhs))
(loop (cons (car rhs) result) lhs (cdr rhs)))
(else
(loop (cons (car lhs) result) (cdr lhs) rhs)))))
Examples:
> (merge-sorted-lists '(1 3 5 7) '(2 4 6 8))
'(1 2 3 4 5 6 7 8)
> (merge-sorted-lists '(7 5 3 1) '(8 6 4 2) #:order >)
'(8 7 6 5 4 3 2 1)
I have such code in Scheme:
(define transpose2 ;1
(lambda (A T) ;2
(if (pair? (car A)) ;3
(transpose2 ;4
(map cdr A) (cons (map car A) T) ;5
) ;6
(reverse T) ;7
) ;8
) ;9
) ;10
I have trouble understanding line nr 5. How creating a pair in this line eventually leads to transposing given matrix?
(define transpose ;1
(lambda (A) ;2
(transpose2 A `()) ;3
) ;4
) ;5
previous function transpose2 is executed by this function and for example:
(display (transpose '((1 2) (3 4) (5 6))))
(display (transpose '((3 2 1) (2 1 1) (3 1 1))))
gives results:
((1 3 5) (2 4 6))
((3 2 3) (2 1 1) (1 1 1))
There are two parameters here: A, your original matrix, and T, your accumulator.
On line 3, we check if the first element of A is a list, e.g. in the form '(1). If so, then we recurse, reducing A by one column with (map cdr A) and accumulating the first member of each A into T. When we finally exhaust our list, we reverse our accumulator and return it.
The code could perhaps be made slightly clearer if the condition on line 3 was replaced with (not (empty? A)).
Example trace:
(transpose '((1 2) (3 4) (5 6)))
-> (transpose2 '((1 2) (3 4) (5 6)) '())
-> (transpose2 (map cdr A) (cons (map car A) T))
-> (transpose2 '((2) (4) (6)) '((1 3 5)))
-> (transpose2 '(() () ()) '((2 4 6) (1 3 5)))
-> (reverse '((2 4 6) (1 3 5)))
-> '((1 3 5) (2 4 6))