General vs simple list - list

What is the difference between simple integer list, general integer list and list in Scheme?
As far as I know list can be created like
(list 1 2 3 4)
But how can I create general or simple integer list?

A list in scheme can hold any data type; essentially they are all 'general'
(list 0 1 '#(0 1) '(0 1) "01" 'zero-one #\0 #\1 '((0) (1)) #t)
You can call something 'simple' if you want to but that term is not part of Scheme's definition.

Related

LISP How to remove element at certain index from a list

I'm trying to learn LISP. I got my way around functions and I wanted to test myself with some.
I was trying to write a function that can remove an element from a list in a given index.
This seems pretty straightforward, but I can't manage to do it.
Example: I have the list (20 8 13 10) and I want to remove the number at index 2.
How would I go about something like this?
It's very easy. This is the base case:
(remove-nth 0 '(2 3)) ; => (3)
And the default case:
(remove-nth 1 '(1 2 3)) ; ==
(cons 1 (remove-nth 0 '(2 3)))
The only thing left for you to do is to actually implement it!
There is a third case. What if the list is nil? In the strictest sense you cannot do the job and you should signal an error or perhaps there isn't anything to do so it's ok to then have it as a base case that evaluates to '() so that (remove-nth 5 '(1 2)) ; ==> (1 2)

Car and Cdr in Scheme

So I've been learn Scheme for school, and have run into a situation using car and cdr series that doesn't quite make sense to me.
So given a list: (define x '(1 2 3 4 5))
How come (caddddr x) spits an error at me, while (cddddr x) returns (5) and (car (cddddr x)) returns 5.
Isn't (caddddr x) the same as (car (cddddr x))?
You can only put a few a's andd's in there :-) check the documentation, between the initial c and the final r there can be between 1 and 4 characters in any combination of a's and d's. If you need to access a specific element beyond that, consider using list-ref, which returns an element given its zero-based index on the list, for example:
(define x '(1 2 3 4 5))
(list-ref x 4)
=> 5
Because the scheme definition goes up to (cddddr pair)‌‌ but not beyond. In the words of the specification for car and cdr and friends: "Arbitrary compositions, up to four deep, are provided." See (for example):
http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_idx_620
And as has been noted elsewhere, list-ref is probably what you want in this case.

Scheme: do these two lists have identical memory representations?

I'm using R5RS standart of Scheme language.
Please have a look at these two objects:
(1 (2 . 3))
(1 2 . 3)
Do they have the same memory representations? Like this (A):
Or the first one is different? Like this (B):
So... What is correct?
They're different. The first list is constructed like this, corresponding to the "B" figure:
(cons 1
(cons (cons 2 3)
'()))
> '(1 (2 . 3))
Whereas the second list structure is constructed like this, which corresponds to the "A" figure:
(cons 1
(cons 2 3))
> '(1 2 . 3)
Also notice that the second one is not a proper list (meaning: a list that ends with null).

How can Scheme's Map function be used to pass two lists to a single function?

I have a question about Scheme. I am fairly new to the language, and my question is fairly general:
Is it possible, without defining a recursive function, to use Map (or something like it) to feed two lists of parameters into a function, producing a single new list from the outputs?
For instance, suppose I had:
(define lst1 (list 1 2 3 4 5))
(define lst2 (list 2 4 6 8 10))
And I wanted to then somehow map the + function, supplying each list as a parameter such that the output would be a new list, lst3:
>lst3
(3 6 9 12 15)
To state the question somewhat more succinctly: how might one most efficiently map a binary function when both parameters are lists?
Thanks for any and all help!
(map + lst1 lst2)
MAP can take any number of lists.
Example:
(map + lst1 lst2 lst1) => (4 8 12 16 20)

In Lisp (Clojure, Emacs Lisp), what is the difference between list and quote?

From reading introductory material on Lisp, I now consider the following to be identical:
(list 1 2 3)
'(1 2 3)
However, judging from problems I face when using the quoted form in both Clojure and Emacs Lisp, they are not the same. Can you tell me what the difference is?
The primary difference is that quote prevents evaluation of the elements, whereas list
does not:
user=> '(1 2 (+ 1 2))
(1 2 (+ 1 2))
user=> (list 1 2 (+ 1 2))
(1 2 3)
For this reason (among others), it is idiomatic clojure to use a vector when describing a literal collection:
user=> [1 2 (+ 1 2)]
[1 2 3]
In Common Lisp, quoted objects are constant literal data. The data is not evaluated. You should not modify this data, as the consequences are undefined. Possible consequences are: modification of shared data, attempt to modify read-only data, an error might be signalled, it might just work, shared data, ...
Literal lists:
'(1 2 3)
Above is a constant list, which will be constructed by the reader and evaluating to itself, because it is quoted. If it appears in Lisp code, a compiler will embed this data somehow in the FASL code.
(quote (1 2 3)) is another way to write it.
(list 1 2 3)
this is a call of the Common Lisp function LIST with three arguments 1, 2 and 3. Each of the arguments will be evaluated. Since they are numbers, they evaluate to themselves. When evaluated the result is a fresh new list (1 2 3).
Data sharing in compiled code
Imagine in a Lisp file the following four definitions:
(defparameter *list1* (list 1 2 3))
(defparameter *list2* (list 1 2 3))
(defparameter *list3* '(1 2 3))
(defparameter *list4* '(1 2 3))
Then we compile and load the file.
! (eq *list3* *list4*) now may evaluate to either T or NIL depending on the implementation and compiler settings !
Reason: in Common Lisp the Lisp compiler may share structure of literal lists (!) if they are similar. The compiler detects that here the lists are similar and will allocate only one list. Both variables *list1* and *list2* the will point to this one list.
All other EQ (object equality) comparisons of two of the above lists will return NIL.
Notations for some other data structures:
'(1 . 2) and (cons 1 2) ; cons cell
'#(1 2 3) and (vector 1 2 3) ; vector
'#S(FOO :a 1 :b 2) and (make-foo :a 1 :b 2) ; structure
One is the literal data and the other is a function call that constructs such a data structure.
Quoted lists (e.g. '(1 2 3)) should be treated carefully (generally as read-only). (see SO answers When to use 'quote in Lisp and When to use 'quote in Lisp).
(list 1 2 3) will "cons" up a fresh list, independent of all others.
You can see an example of a pitfall of using quoted lists in the manual for nconc.
And, as you probably know, when you call 'list - the arguments will obviously be evaluated versus the contents of a quoted list. And 'quote takes a single argument, versus 'lists variable number of arguments.
(list (+ 1 2) 3) --> (3 3)
(quote ((+ 1 2) 3)) --> ((+ 1 2) 3)
Their relation can be analogous to function invocation with 'function name' and funcall.
When you have no idea what function you'll get at runtime, you use funcall.
When you have no idea what element you may get at runtime, your use list.
For people like me who get confused because of existence of backquote and count it as quote tacitly.
backquote is not quote
It's a reader-macro that expands into quote, list or others:
(macroexpand ''(1 2));=> '(1 2)
(macroexpand '`(1 2));=> '(1 2)
(macroexpand '`(1 ,2));=> (list 1 2)
(macroexpand '`(1 ,#foo));=> (cons 1 foo)
(macroexpand '`(1 ,#foo 2));=> (cons 1 (append foo '(2)))