chong.franklin
2/1/2016 10:17:00 AM
On Monday, February 1, 2016 at 6:08:08 PM UTC+8, Jussi Piitulainen wrote:
> franklin writes:
>
> > On Monday, February 1, 2016 at 5:29:09 PM UTC+8, Piotr Chamera wrote:
> >> W dniu 2016-02-01 o 08:01, franklin pisze:
> >> > This quote is from SICP that I think is talking about
> >> > pointers/references in programming languages..
> >> >
> >> > "As we have seen, pairs provide a primitive "glue" that we can use to
> >> > construct compound data objects. Figure 2.2 shows a standard way to
> >> > visualize a pair--in this case, the pair formed by (cons 1 2). In
> >> > this representation, which is called box-and-pointer notation, each
> >> > object is shown as a pointer to a box. The box for a primitive object
> >> > contains a representation of the object. For example, the box for a
> >> > number contains a numeral. The box for a pair is actually a double
> >> > box, the left part containing (a pointer to) the car of the pair and
> >> > the right part containing the cdr."
> >> >
> >> > In this case the book is talking about pairs (E.G. (cons 1 2)) and
> >> > how they are represented. However we can also use pairs to construct
> >> > a list like this:
> >> >
> >> > (cons 1 (cons 2 '()))
> >> >
> >> > Though box-and-pointer-notation is just a notation and useless, I
> >> > think this looks a lot like a linked list. As I understand it, a
> >> > linked list is a data structure that contains a value and a pointer
> >> > to another linked list. Having said that I think cons can be
> >> > constructed like a linked list. I'm confused by:
> >> >
> >> > "The box for a pair is actually a double box, the left part
> >> > containing (a pointer to) the car of the pair and the right part
> >> > containing the cdr."
> >> >
> >> > I originally thought the pointer should be on cdr because that would
> >> > be the next list if we were constructing a list through pairs.
> >>
> >> Cons is general data structure with two identical slots.
> >> You can build other data structures with it, like above mentioned linked
> >> list, but also tree, and others. For example such a tree:
> >>
> >> (cons (cons 2 3)
> >> (cons (cons 4 "abc")
> >> 'xyz))
> >>
> >> or list alike structure like this (cons (cons '() 1) 2)
> >>
> >> Both slots in cons are identical and both can hold reference to any
> >> other object inside. Objects 1, 2, "abc", 'xyz and conses above are
> >> probably stored elsewhere, not in cons itself - cons only holds
> >> references (pointers) to them.
> >>
> >> > I think this might be a different kind of pointer all together. What
> >> > exactly does a pointer mean in this case? The only pointer I know is
> >> > pointers used in c. Does SICP even mention anything about c
> >> > pointers?
> >>
> >> cons is something like structure with two void pointers C:
> >>
> >> struct cons {
> >> void *car;
> >> void *cdr;
> >> };
> >>
> >> You can build anything with this...
> >
> > Thanks. This got me interested. I haven't read the whole thing, will
> > SICP talk about pointers in later chapters?
>
> Yes.
Cool! What section?