how can i append to a list without creating a dotted pair - scheme

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) "|")

Related

Is append the identity function for map?

In doing some tests I've noticed that append always gives me the same output as input when using map:
#lang sicp
(map append '(1 2 3 4 5))
(map (lambda (x) x) '(1 2 3 4 5))
; (1 2 3 4 5)
; (1 2 3 4 5)
(map append '((1 2)(3 4)))
(map (lambda (x) x) '((1 2)(3 4)))
; ((1 2) (3 4))
; ((1 2) (3 4))
That seems pretty neat/unexpected to me. Is this in fact the case? If so, how does the append work to give the identity property?
The append procedure takes zero or more list arguments, and a final argument that can be any object. When the final argument is a list, the result of appending is a proper list. When the final argument is not a list, but other list arguments have been provided, the result is an improper list. When only one argument is provided, it is just returned. This behavior with one argument is exactly the behavior of an identity procedure.
> (append '(1 2) '(3))
(1 2 3)
> (append '(1 2) 3)
(1 2 . 3)
> (append '(1 2))
(1 2)
> (append 3)
3
The call (map append '(1 2 3 4 5)) is equivalent to:
> (list (append 1)
(append 2)
(append 3)
(append 4)
(append 5))
(1 2 3 4 5)
Here, append is just acting as an identity procedure, as described above.

Why the same data is printed differently when built with list or cons? [duplicate]

This question already has answers here:
Dot notation in scheme
(3 answers)
Closed 5 years ago.
I tried to reproduce the same data layout by using different combinations of list and cons. In MIT Scheme, the same data layout seems to be printed differently depending on how it was built.
In example,
> (cons 1 (cons 2 3))
(1 2 . 3)
> (list 1 (cons 2 3))
(1 (2 . 3))
> (cons 1 (list 2 3))
(1 2 3)
> (list 1 (list 2 3))
(1 (2 3))
The underlying data should always be the same. Why does MIT Scheme represents it differently? Is the underlying arrangements of pairs really the same every time?
Each of the inputs is building a different output, so they're printed differently. Notice that the following expressions are equivalent:
(cons 1 (cons 2 3))
; (1 2 . 3)
(list 1 (cons 2 3))
(cons 1 (cons (cons 2 3) '()))
; (1 (2 . 3))
(cons 1 (list 2 3))
(cons 1 (cons 2 (cons 3 '())))
; (1 2 3)
(list 1 (list 2 3))
(cons 1 (cons (cons 2 (cons 3 '())) '()))
; (1 (2 3))
Remember: a list is defined if and only if the cdr part of each cons is also a cons cell, or an empty list.

Counting adjacent duplicates in scheme and prolog

I am new to scheme and prolog and have done only a few basic problems.
I need to write a function in scheme and prolog for counting adjacent recurrences in a list.
Example: (Count '(1, 1, 1, 2, 3, 3, 1)) gives ((3 1) (1 2) (2 3) (1 1))
I do not want the code as this is homework but any hints or how to think about this problem would be appreciated as I don't know where to start.
Let's look at a hypothetical function loop:
(loop todo done)
that rewrites the list in todo to you wanted format
and done contains the part already handled.
(loop '(1 1 1 2 3 3 1) '())
(loop '(1 1 2 3 3 1) '((1 1))
(loop '(1 2 3 3 1) '((1 2))
(loop '(2 3 3 1) '((1 3))
(loop '(3 3 1) '((2 1) (1 3))
(loop '(3 1) '((3 1) (2 1) (1 3))
(loop '(1) '((3 2) (2 1) (1 3))
(loop '() '((1 1) (3 2) (2 1) (1 3))
'((1 1) (3 2) (2 1) (1 3)
Given such a function loop you can write
(define (count xs)
(reverse loop xs '())
Now in order to implement loop you need to consider these cases:
1) todo is empty
2) done is empty
3) the first element of todo is different from the first element of done
3) the first element of todo is the same as the first element of done

printing pairs from a list in scheme

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)))

Scheme cons and length

I'm studying scheme and I have just encountered my first problem:
(define x (cons (list 1 2) (list 3 4)))
(length x)
3
why the output is 3 and not 2? I have displayed x
((1 2) 3 4)
why is like that and not ((1 2) . (3 4)) ?
Thanks.
Maybe it's easier to see this way.
You have:
(cons (list 1 2) (list 3 4))
If you
(define one-two (list 1 2))
you have
(cons one-two (list 3 4))
which is equivalent to
(cons one-two (cons 3 (cons 4 '())))
or
(list one-two 3 4)
which is
((1 2) 3 4)
List is the basic data structure of scheme.
Cons is used for making a pair of objects. List is the chain of cons.
eg. list (1 2 3 4) is same as (cons 1(cons 2(cons 3(cons 4 '())))).
See the block pointer representation for make it clear

Resources