Join or concatenate two lists in Scheme - scheme

I'm trying to join the lists '(1 2 3) and '(4 5 6) so that they become '(1 2 3 4 5 6). In Clojure for example, this function is called concat.
I tried searching for the answer on the internet but could only find people trying to implement more advanced merge strategies for the lists.

In scheme this function is called append.

Related

MIT Scheme - Merge Sort + Timing Execution

I've implemented my own merge sort in MIT Scheme. I want to test it against the builtin merge-sort and compare times; however, I don't know how to get the run time of both. Also how do you increase the stack size/recursion depth as i'm testing up to 1 million elements.
There's a bunch of timing procedures in MIT Scheme, check the documentation. In particular, try this one:
(with-timings
(lambda ()
(merge-sort '(1 2 3 4 5) >))
(lambda (run-time gc-time real-time)
(write (internal-time/ticks->seconds run-time))
(write-char #\space)
(write (internal-time/ticks->seconds gc-time))
(write-char #\space)
(write (internal-time/ticks->seconds real-time))
(newline)))
The built-in sort shouldn't have a problem with one million elements, if your own implementation is a good one, it shouldn't have problems producing a result with that data size.

Lisp graph data structures

I'm reading Jorge Gajon's Trees as Linked Lists in Common Lisp, which includes a description of a tree done in Lisp. The author gives this basic example:
Then gives a Lisp list representaton:
(1 (2 6 7 8) 3 (4 (9 12)) (5 10 11))
where position implies hierarchy level: 1 is first in the list, i.e., it's at the top, 2 is at the next level, but it's the top of its level, etc. But then he gives this caveat:
PLEASE NOTE, that if you need to represent trees in a production program you shouldn’t use lists as described here unless you have a good reason. This is only an exercise in understanding how cons cells work.
All right, how should one represent the tree data structure in "production" code? BTW, I'd also like to see an example of an acyclic directed graph, i.e., something tree-like that also has "multiple parent" capabilities. For example in the diagram above, 8 is a child of 2, but also 3. I'm guessing something like this:
(1 (2 6 7 8) (3 8) (4 (9 12)) (5 10 11))
but it seems like I've created a "shadow" twin of 8 and not really relating how the 8 that is the child of 3 is the same 8 that also has 2 as a parent. This problem gets worse if I wanted to have 3 as 12's parent.
(1 (2 6 7 8) (3 8 12) (4 (9 12)) (5 10 11))
The fact that 12 is in a lower hierarchy gets lost in the shuffle, so to speak.
Is there a good and proper treatment (book, etc.) of data structures in the Lisp/Scheme/Clojure world? I've only found one-off stuff like this.
Assuming you have quicklisp installed, and you should, then consider the libaries enumerated by: (ql:system-apropos "graph"), and also try cliki http://cliki.net/site/search?query=graph
The 'production' code would use CLOS (the Common Lisp Object System) to define a NODE class and operations over the graph/tree.

Turning list-of-lists into set of lists in scheme for macro expansion body?

Is there a general way to take a list of items and flatten it to depth zero so that they can be spliced into a macro expansion? For instance:
((+ 1 2) (+ 3 4) (+ 4 5)) -> (+ 1 2) (+ 3 4) (+ 4 5)
Note: This is based on Eli Barzilay's comment that never ended up getting turned into an answer. That's why it's community wiki.
If you want a sequence of expressions for the expansion of a macro, then you need to wrap them in a begin. As for the flattening, you can do that with a ... after the thing that you want to splice up. This is assuming that you're using syntax-rules or syntax-case, of course.

Operating on multiple lists?

On page 224 of Common Lisp: A Gentle Introduction to Symbolic Computation this example is given with the output.
> (mapcar #'(lambda (x y) (list x 'gets y))
'(fred wilma george diane)
'(job1 job2 job3 job4))
((FRED GETS JOB1)
(WILMA GETS JOB2)
(GEORGE GETS JOB3)
(DIANE GETS JOB4))
Is there a way to do the same thing in Emacs Lisp? The other example is also interesting because only 3 results, the number of elements in the shortest list, are produced.
> (mapcar #'+ '(1 2 3) '(10 20 30 40 50))
(11 22 33)
Emacs has mapcar* in the cl package which does exactly that. Here is the documentation:
Apply FUNCTION to each element of SEQ, and make a list of the results.
If there are several SEQs, FUNCTION is called with that many arguments,
and mapping stops as soon as the shortest list runs out. With just one
SEQ, this is like mapcar. With several, it is like the Common Lisp
mapcar function extended to arbitrary sequence types.

Self-referential data structures in Lisp/Scheme

Is there a way to construct a self-referential data structure (say a graph with cycles) in lisp or scheme? I'd never thought about it before, but playing around I can find no straightforward way to make one due to the lack of a way to make destructive modification. Is this just an essential flaw of functional languages, and if so, what about lazy functional languages like haskell?
In Common Lisp you can modify list contents, array contents, slots of CLOS instances, etc.
Common Lisp also allows to read and write circular data structures. Use
? (setf *print-circle* t)
T
; a list of two symbols: (foo bar)
? (defvar *ex1* (list 'foo 'bar))
*EX1*
; now let the first list element point to the list,
; Common Lisp prints the circular list
? (setf (first *ex1*) *ex1*)
#1=(#1# BAR)
; one can also read such a list
? '#1=(#1# BAR)
#1=(#1# BAR)
; What is the first element? The list itself
? (first '#1=(#1# BAR))
#1=(#1# BAR)
?
So-called pure Functional Programming Languages don't allow side-effects. Most Lisp dialects are not pure. They allow side-effects and they allow to modify data-structures.
See Lisp introduction books for more on that.
In Scheme, you can do it easily with set!, set-car!, and set-cdr! (and anything else ending in a bang ('!'), which indicates modification):
(let ((x '(1 2 3)))
(set-car! x x)
; x is now the list (x 2 3), with the first element referring to itself
)
Common Lisp supports modification of data structures with setf.
You can build a circular data structure in Haskell by tying the knot.
You don't need `destructive modification' to construct self-referential data structures; e.g., in Common Lisp, '#1=(#1#) is a cons-cell that contains itself.
Scheme and Lisp are capable of making destructive modifications: you can construct the circular cons above alternatively like this:
(let ((x (cons nil nil)))
(rplaca x x) x)
Can you let us know what material you're using while learning Lisp/Scheme? I'm compiling a target list for our black helicopters; this spreading of misinformation about Lisp and Scheme has to be stopped.
Yes, and they can be useful. One of my college professors created a Scheme type he called Medusa Numbers. They were arbitrary precision floating point numbers that could include repeating decimals. He had a function:
(create-medusa numerator denominator) ; or some such
which created the Medusa Number that represented the rational. As a result:
(define one-third (create-medusa 1 3))
one-third => ; scheme hangs - when you look at a medusa number you turn to stone
(add-medusa one-third (add-medusa one-third one-third)) => 1
as said before, this is done with judicious application of set-car! and set-cdr!
Not only is it possible, it's pretty central to the Common Lisp Object System: standard-class is an instance of itself!
I upvoted the obvious Scheme techniques; this answer addresses only Haskell.
In Haskell you can do this purely functionally using let, which is considered good style. One nice example is regexp-to-NFA conversion. You can also do it imperatively using IORefs, which is considered poor style as it forces all your code into the IO monad.
In general Haskell's lazy evaluation lends itself to lovely functional implementations of both cyclic and infinite data structures. In any complex let binding, all things bound may be used in all definitions. For example translating a particular finite-state machine into Haskell is a snap, no matter how many cycles it may have.
CLOS example:
(defclass node ()
((child :accessor node-child :initarg :child)))
(defun make-node-cycle ()
(let* ((node1 (make-instance 'node))
(node2 (make-instance 'node :child node1)))
(setf (node-child node1) node2)))
Tying the Knot (circular data structures in Haskell) on StackOverflow
See also the Haskell Wiki page: Tying the Knot
Hmm, self referential data structures in Lisp/Scheme, and SICP streams are not mentioned? Well, to summarize, streams == lazily evaluated list. It might be exactly the kind of self reference you've intended, but it's a kind of self reference.
So, cons-stream in SICP is a syntax that delays evaluating its arguments. (cons-stream a b) will return immediately without evaluating a or b, and only evaluates a or b when you invoke car-stream or cdr-stream
From SICP, http://mitpress.mit.edu/sicp/full-text/sicp/book/node71.html:
>
(define fibs
(cons-stream 0
(cons-stream 1
(add-streams (stream-cdr fibs)
fibs))))
This definition says that fibs is a
stream beginning with 0 and 1, such
that the rest of the stream can be
generated by adding fibs to itself
shifted by one place:
In this case, 'fibs' is assigned an object whose value is defined lazily in terms of 'fibs'
Almost forgot to mention, lazy streams live on in the commonly available libraries SRFI-40 or SRFI-41. One of these two should be available in most popular Schemes, I think
I stumbled upon this question while searching for "CIRCULAR LISTS LISP SCHEME".
This is how I can make one (in STk Scheme):
First, make a list
(define a '(1 2 3))
At this point, STk thinks a is a list.
(list? a)
> #t
Next, go to the last element (the 3 in this case) and replace the cdr which currently contains nil with a pointer to itself.
(set-cdr! (cdr ( cdr a)) a)
Now, STk thinks a is not a list.
(list? a)
> #f
(How does it work this out?)
Now if you print a you will find an infinitely long list of (1 2 3 1 2 3 1 2 ... and you will need to kill the program. In Stk you can control-z or control-\ to quit.
But what are circular-lists good for?
I can think of obscure examples to do with modulo arithmetic such as a circular list of the days of the week (M T W T F S S M T W ...), or a circular list of integers represented by 3 bits (0 1 2 3 4 5 6 7 0 1 2 3 4 5 ..).
Are there any real-world examples?

Resources