I am writing an interpreter for R7RS Scheme to gain a more complete understanding of the Scheme programming language.
From my understanding, eqv? must return #t if both list arguments denote the same location in memory. However, I am not sure if the cdr of a list must always be eqv:
(define l '(a b c))
(eqv? (cdr l) (cdr l)) ; #t of #f?
The missing part of my knowledge is whether or not the cdr of a particular list must always point to a particular location. For a particular list, must cdr always return an identical sublist every time it is called on the list, or can it return a completely new sublist?
(I understand that I can test this empirically using existing Scheme interpreters, but I am mainly interested in what the standard mandates).
eq? is pointer equality (symbol, boolean, empty list)
eqv? is #t for everything that is eq? and the same primitive value (number, char)
equal? is #t for everything that is eqv? and values that look the same
cdr is an accessor. (cdr l) will return the same pointer and thus (eq? (cdr l) (cdr l)) ; ==> #t and so will eqv? and equal? since they are guaranteed #t when a lower level equality predicate is.
Note that is is not the other way around. eg. (equal? "test" "test"); ==> #t but (eqv? "test" "test") can be either #f or #t. The cause of the different behavior is if you reuse constant data when you read the code rather then create new.
It's common to store primitive values in the pointer. Eg. on a 64 bit machine the last 3 bits is always 0 since we access the words aligned. Scheme implementation usually then code 0-7 to indicate type and often when it's 0 the rest of the bits isn't a location at all rather than a number embedded in the pointer. This way you can have a list (1 2 3) that uses 6 words. 3 pairs of 2 words each but no memory used for the numbers when they fit the 61bit size. This is why numbers and char are often eq? while not guaranteed in the report.
Related
Can you explain why the first one is false and the second one is true?
And how this works? Thanks.
(eq? '(1 2 3) '(1 2 3)) ;False
(eq? '() '()) ;True
There's only one empty list, so all uses of () refer to that list, and it's eq? to itself. The Scheme Specification description of the storage model says:
Notwithstanding this, it is understood that the empty list cannot be
newly allocated, because it is a unique object.
and the specification of eqv? (which is referenced by the eq? description) says that two objects are equivalent if
obj1 and obj2 are both the empty list
But when you create a non-empty list, it creates a fresh one every time, and they're not eq? to each other even if they contain the same elements.
Quoted from TSPL3:
[..] Two objects are considered identical if they are represented internally by the same pointer value
[..] The empty list () is identical to itself wherever it appears.
[..] Two pairs, vectors, or strings created by different applications of cons, vector, string, etc., are distinct.
If you write instead
(let ((x '(1 2 3)))
(eq? x x))
it will be #t.
In The Little Schemer (4th Ed.) it is claimed that a list for which null? is false contains at least one atom, or so I understand from my reading of the text.
This doesn't make sense to me, since (atom '()) is false, and we can stick those into a list to make it non-null:
> (null? '(()))
#f
So my question is, is this a mistake in my reading, or a matter of definitions? Since it's not in the errata I assume such a well-studied book wouldn't have a mistake like this.
If we considered (()) to be the same as (() . ()) or even (cons '() '()) and then considered cons an atom then I could see how you can get there, but I don't think that's what's going on.
(this was tested in Racket 7.0, with the definition of atom? given in the book, i.e.
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
I know this doesn't cover funny Racket features, but should be sufficient here.)
lat is assumed to be a list of atoms at that point in the book.
If it's not empty, by definition it contains some atoms in it.
It's not about Lisp, it's about the book's presentation.
I think lat indicates list of atoms. Thus if lat is not null?, then it needs to contain at least one atom.
There is a procedure called lat? defined as such:
(define lat?
(lambda (l)
(cond
((null? l) #t)
((atom? (car l))
(lat? (cdr l)))
(else #f))))
(lat? '(()) ; ==> #f so by definition '(()) is not a lat and thus the statement does not apply to that list.
A list can contain any type of elements, including empty and other lists, both which are not atoms. lat is a restricted to a flat list with only atomic elements.
As a concept an “atom” is something that can not be broken into smaller parts. A number 42 is an atom, a list (42 43) is not an atom since it contains two smaller parts (namely the numbers 42 and 43). Since an empty list does not contain any smaller parts, it is by this logic an atom.
Now let’s attempt to implement an atom? predicate, that determines whether it’s input is an atom.
(define (atom? x)
(cond
[(number? x) #t]
[(symbol? x) #t]
[(char? x) #t]
...
[else #f]))
Here the ... needs to be replaced with a test for every atomic data type supported by the implementation. This can potentially be a long list. In order to avoid this, we can try to be clever:
(define (atom? x)
(not (list? x)))
This will correctly return false for non-empty lists, and true for numbers, characters etc. However it will return false for the empty list.
Since it is up to the authors of the book to define the term “atom” (the word does not appear in the language standard) they might have opted for the above simple definition.
Note that the definition as non-list is misleading when the language contains other compound data structures such as vectors and structures. If I recall correctly the only compound data structure discussed in the book is lists.
I want to compare two lists in elisp using (cl-every #'eq list1 list2). However, this can return t if one of the lists is longer than the other, and I don't want that. I could call length on the lists, but then I'm traversing each list twice, which is needlessly inefficient. Is there a function like cl-every that also checks for equal lengths as well?
OOTB
I don't think there is such a function OOTB.
Roll your own
I don't think it is hard to implement one, just modify cl-every.
Length
Note that length is implemented in C and, unless your lists are huge, should not noticeably affect performance:
(defun list-elements-eq (l1 l2)
(and (= (length l1)
(length l2))
(every #'eq l1 l2)))
Equal
You can also use equal for your lists: since equal starts with testing for eq, it will be a weaker relationship than what you want.
Beware that in Common Lisp equal may not terminate for circular structures.
Emacs Lisp is "smarter": it detects cicularity:
(setq x (list 1))
(setcdr x x)
(setq y (list 1))
(setcdr y y)
(eq x y)
(equal x y)
Debugger entered--Lisp error: (circular-list #1=(1 . #1#))
equal(#1=(1 . #1#) #2=(1 . #2#))
(arguably, it should returns t instead).
Common Lisp
Generally speaking, Common Lisp is a better language for "heavy lifting".
If your lists are huge, you might want to use it instead of Emacs Lisp:
(defun list-elements-eq (l1 l2)
(if (endp l1)
(endp l2)
(and (not (endp l2))
(eq (pop l1) (pop l2))
(list-elements-eq l1 l2))))
This is tail-recursive, and any decent CL will optimize recursion away (possibly depending on the optimize setting).
You don't want to use deep recursion in ELisp (see Why is there no tail recursion optimization in Emacs lisp, not but like other scheme?).
I have an assignment question that asks me to write a function, using lambda, that consumes a list of numbers and removes all but the first occurrence of each number. My function is I think quite nice, with the exception that it produces the wrong result! It produces a list that contains the last occurrence of each number. It produces a list with the same values as the proper list, just in different order. Here's my code:
(define (remove-duplicates numlist)
(foldr (lambda (a b)
(cond
[(not (member? a b)) (cons a b)]
[else b])) empty numlist))
I've tried using foldl instead of foldr but, not surprisingly, it produces the correct list, but in reverse. Is there a way of producing the correct list without resorting to reversing the list created by foldl with another lambda expression?
Please keep in mind, this is homework so no explicit answers please.
Thanks everyone!
Think about how foldr behaves, semantically. It starts at the rightmost element of the list, and performs the fold, moving leftwards. That means that in your lambda function, a represents the element directly to the left of the thus-far-folded list, and b represents the result of folding everything to the right of the list element a. With this in mind, consider:
[(not (member? a b)) (cons a b)]
[else b]
You check whether the list b already contains a. If it does, then you discard a, keeping b as it is. This explains why your code keeps the last (rightmost) occurrence, and not the first. If you wish to perform a right fold, then, you must change your logic. You need to somehow "purge" b of the offending element a that it contains.
[(not (member? a b)) (cons a b)]
[else (cons a (purge a b))]
This way, you will eventually keep only the leftmost, rather than the rightmost, occurrence of each unique element. It may be wise to perform member? and purge simultaneously, since they both must traverse the list; this would require additional refactoring.
Note that, since the function takes O(n2) time anyways, it really doesn't hurt the time complexity to add a O(n) reverse to the foldl version.
In Scheme, the function (map fn list0 [list1 .. listN]) comes with the restriction that the lists must have the same number of elements. Coming from Python, I'm missing the freedom of Python list comprehensions, which look a lot like map above, but without this restriction.
I'm tempted to implement an alternative "my-map", which allows for lists of differing size, iterating through the first N elements of all lists, where N is the length of the shortest list.
For example, let num be 10 and lst be (1 2 3). With my-map, I hope to write expressions like:
(my-map + (circular-list num) lst)))
And get:
(11 12 13)
I have an easier time reading this than the more conventional
(map + (lambda (arg) (+ num arg)) lst)
or
(map + (make-list (length lst) num) lst)
Two questions:
As a Scheme newbie, am I overlooked important reasons for the restriction on `map`?
Does something like `my-map` already exist in Scheme or in the SRFIs? I did take a look at srfi-42, but either it's not what I'm looking for, or it was, and it wasn't obvious.
First, note that map does allow empty lists, but of course if there's one empty list then all of them should be empty.
Second, have a look at the srfi-1 version of map -- it is specifically different from the R5RS version as follows:
This procedure is extended from its R5RS specification to allow the arguments to be of unequal length; it terminates when the shortest list runs out.
Third, most Scheme programmers would very much prefer
(map (lambda (arg) (+ num arg)) lst)
My guess is that Scheme is different from Python in a way that makes lambda expressions become more and more readable as you get used to the language.
And finally, there are some implementations that come with some form of a list comprehension. For example, in Racket you can write:
(for/list ([arg lst]) (+ num arg))