I've read and somewhat understand Use of lambda for cons/car/cdr definition in SICP. My problem is understanding the why behind it. My first problem was staring and staring at
(define (cons x y)
(lambda (m) (m x y)))
and not understanding how this function actually did any sort of consing. Consing as I learned it from various Lisp/Scheme books is putting stuff in lists, i.e.,
(cons 1 ()) => (1)
how does
(define (cons x y)
(lambda (m) (m x y)))
do anything like consing? But as the light went on in my head: cons was only sort of a placeholder for the eventual definitions of car and cdr. So car is
(define (car z)
(z (lambda (p q) p)))
and it anticipates an incoming z. But what is this z? When I saw this use:
(car (cons 1 2))
it finally dawned on me that, yes, the cons function in its entirety is z, i.e., we're passing cons to car! How weird!
((lambda (m) (m 1 2)) (lambda (p q) p)) ; and then
((lambda (p q) p) 1 2)
which results in grabbing the first expression since the basic car operation can be thought of as an if statement where the boolean is true, thus, grab the first one.
Yes, all lists can be thought of as cons-ed together expressions, but what have we won by this strangely backward definition? It's as if any initial, stand-alone definition of cons is not germane. It's as if uses of something define that something, as if there's no something until its uses circumscribe it. Is this the primary use of closures? Can someone give me some other examples?
but what have we won by this strangely backward definition?
The point of the exercise is to demonstrate that data structures can be defined completely in terms of functions; that data structures are not necessary as a primitive construct in a language -- if you have functions (that are closures), that's sufficient. This shows the power of functions, and is probably mind-boggling to someone from outside of functional programming.
It's not that in a real project we would actually define data structures this way. It would be more efficient to use language-provided data structure constructs. But it's important to know that we can do it this way. In computer science, it's useful to be able to "reduce" one construct (data structures) into another construct (functions) so that if we prove something about the second construct, it applies to the first one too.
Related
Hi I'm trying to learn Scheme and I was working on an example from a university website:
https://courses.cs.washington.edu/courses/cse341/05au/lectures/scheme-side-effects.html
The example is something along these lines:
(define circ '(a b))
(set-cdr! (cdr circ) circ)
and this is the output:
=> #0=(a b . #0#)
I don't understand what this means. The code defines a variable called circ as a list with 2 elements (a b).
set-cdr! mutates the cdr of this list [which is (b '())] and changes it to circ (which is (a b)).
So the output I expected here was (a (a b)) but I got this weird hashtag thing instead.
I'm using DrRacket IDE with R5RS scheme set as the language.
What does this hashtag stuff mean? Is it maybe creating a pointer to itself like (a [pointer to circ]) in which case it would be like some kind of infinite loop or something?
I mean if I do this:
(define x '(a b))
(set-cdr! x 'c)
x
=>(a . c) ; is the output
this is easy to understand as set-cdr! replaces the (b '()) with 'c and getting rid of the '() at the end is why I get a dotted pair back instead of a list. But this isn't in-line with the earlier example.
anyway if anybody cares to fill me in, let me know. Thanks in advance.
You are correct in thinking that the operation is the creation of a circular list.
Drawing a list through its cons cells, this is the situation after the define:
and this is the situation after the set-cdr!:
Note that the modification is on the cdr of the cdr of circ (so on the cdr of the second cell). The notation #0=(a b . #0#) in lisp languages describes an improper list where the last cdr is equal to the list itself, producing a circular data structure (i.e. a data structure with a “loop”).
In Racket (and other Schemes, from what I can tell), the only way I know of to check whether two things are not equal is to explicitly apply not to the test:
(not (= num1 num2))
(not (equal? string1 string2))
It's obviously (not (that-big-of-deal?)), but it's such a common construction that I feel like I must be overlooking a reason why it's not built in.
One possible reason, I suppose, is that you can frequently get rid of the not by using unless instead of when, or by switching the order of the true/false branches in an if statement. But sometimes that just doesn't mimic the reasoning that you're trying to convey.
Also, I know the negated functions are easy to define, but so is <=, for example, and that is built in.
What are the design decisions for not having things like not-equal?, not-eqv?, not-eq? and != in the standard library?
First, you are correct that it is (not (that-big-of-a-deal?))1
The reason Racket doesn't include it out of the box is likely just because it adds a lot of extra primitives without much benefit. I will admit that a lot of languages do have != for not equal, but even in Java, if you want to do a deep equality check using equals() (analogous to equal? in Racket), you have to manually invert the result with a ! yourself.
Having both <= and > (as well as >= and <) was almost certainly just convenient enough to cause the original designers of the language to include it.
So no, there isn't any deep reason why there is not any shortcut for having a not-eq? function built into Racket. It just adds more primitives and doesn't happen to add much benefit. Especially as you still need not to exist on its own anyway.
1I love that pun by the way. Have some imaginary internet points.
I do miss not having a not= procedure (or ≠ as mentioned in #soegaard's comment), but not for the reasons you think.
All the numeric comparison operators are variadic. For example, (< a b c d) is the same as (and (< a b) (< b c) (< c d)). In the case of =, it checks whether all arguments are numerically equal. But there is no procedure to check whether all arguments are all unequal—and that is a different question from whether not all arguments are equal (which is what (not (= a b c d)) checks).
Yes, you can simulate that procedure using a fold. But still, meh.
Edit: Actually, I just answered my own question in this regard: the reason for the lack of a variadic ≠ procedure is that you can't just implement it using n-1 pairwise comparisons, unlike all the other numeric comparison operators. The straightforward approach of doing n-1 pairwise comparisons would mean that (≠ 1 2 1 2) would return true, and that's not really helpful.
I'll leave my original musings in place for context, and for others who wonder similar things.
Almost all of the predicates are inherited by Scheme, the standard #!racket originally followed. They kept the number of procedures to a minimum as a design principle and left it to the user to make more complex structures and code. Feel free to make the ones you'd like:
(define not-equal? (compose1 not equal?))
(define != (compose1 not =))
; and so on
You can put it in a module and require it. Keep it by convention so that people who read you code knows after a minute that everything not-<known predicate> and !-<known-predicate> are (compose not <known-predicate>)
If you want less work and you are not after using the result in filter then making a special if-not might suffice:
(define-syntax-rule (if-not p c a) (if p a c))
(define (leafs tree)
(let aux ((tree tree) (acc 0))
(if-not (pair? tree)
(+ acc 1) ; base case first
(aux (cdr tree)
(aux (car tree) acc)))))
But it's micro optimizations compared to what I would have written:
(define (leafs tree)
(let aux ((tree tree) (acc 0))
(if (not (pair? tree))
(+ acc 1) ; base case first
(aux (cdr tree)
(aux (car tree) acc)))))
To be honest if I were trying to squeeze out a not I would just have switched them manually since then optimizing speed thrumps optimal readability.
I find that one easily can define != (for numbers) using following macro:
(define-syntax-rule (!= a b)
(not(= a b)))
(define x 25)
(!= x 25)
(!= x 26)
Output:
#f
#t
That may be the reason why it is not defined in the language; it can easily be created, if needed.
I am currently trying to write a function that finds a value in racket using DrRacket. With some help I came up with this below. However I need someone to explain to me what the difference is between cadr and caddr? Also in DrRacket how would I create a BST? Is it similar to making a list?
(define (find-val bst elt)
(cond ((null? bst) #f)
((< elt (car bst))
(find-val (cadr bst) elt))
((> elt (car bst))
(bst (caddr bst) elt))
((equal? elt (car bst))
#t)))
For the first part of your question,
(cadr x)
is equivalent to:
(car (cdr x))
and
(caddr x)
is equivalent to:
(car (cdr (cdr x)))
Did you notice the pattern? each d between the c and r is a shorthand for a cdr, and each a between the c and r is a shorthand for a car, in left-to-right order.
Regarding the second part of your question, there's a very detailed explanation of how to represent a BST in the book SICP, section §2.3.3 under the title sets as binary trees. There you'll find the required procedures for creating and manipulating a tree.
In Racket, we can use struct to define the shape of structured values. For example:
(struct person (name age))
defines a person structure. It allows us to create 2-field structured values, such as:
(define p1 (person "danny" 33))
(define p2 (person "richie" 31))
We can access individual fields of a structured value by using its selector functions.
;; p: person -> void
(define (say-hi p)
(printf "hi, my name is ~a, and I am ~a years old\n"
(person-name p)
(person-age p)))
(say-hi p1)
(say-hi p2)
There are other ways of creating structured values, such as using plain list structure. But using struct is preferable for most applications in Racket. Here are several reasons why:
Access to a field in a struct is fast--a single dereference. In list representation, field access involves walking the list structure, which is somewhat more expensive.
The type checks in struct selectors can more quickly detect mistakes in passing the wrong kind of data around, and produce the appropriate error messages.
Creating a binary search tree, then, involves defining a structure to represent the individual nodes of the tree. It also requires following fairly rigorous rules during construction, since the nodes are ordered in a peculiar way to make searching through the structure potentially faster than sequential lists.
An algorithms textbook should go over some of the potential rulesets we can follow to create BSTs that guarantee good balance among the nodes. These include things like red-black trees, which can have peculiar rules on how to maintain the structure of the tree as inserts push values into the tree. There is nothing really Racket specific to how it happens: it's just the general requirements we need to follow to make the balance work out.
Matt Might has written a very nice article on red-black trees implemented in Racket.
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))
Finally taking the plunge to learn a Lisp dialect (Scheme), I have encountered two definitions of a list -
"Either the empty list or a pair whose cdr is a list".
"A collection of S-Expressions enclosed by parentheses".
Are these definitions equivalent?
They're as equivalent as {'a','b','c'} and "abc"
The former is the machine's logical representation of a list, the latter is how you represent it in your code.
And in scheme, you can pretty much treat everything as a list :) (Someone's going to downvote me for that, but I found it to be true when trying to think scheme-esque.)
I'm going to bring out my favourite dog-and-pony show!
(source: hedgee.com)
This corresponds to the following:
(let ((s5 (sqrt 5)))
(/ (- (expt (/ (1+ s5) 2) n)
(expt (/ (- 1 s5) 2) n)) s5))
The diagram illustrates your first statement (the empty-list object is denoted as a black box). The code snippet illustrates your second. :-)