It's graduation day. We're going to move from the beginning
student launguage to the next most advanced language.
It isn't a big move, but it's a good move.
The way you do is you go into the lower corner of Dr.
Racket here, and you switch from beginning student to.
Beginning student with list abbreviations.
Now what are list abbreviations? Well, by this point you probably gotten a
little annoyed at having to write something like cons a, cons b, cons c,
empty. When all you really wanted to do was make
a list of three items A, B and C. And what lists of abbreviations do, is
they let you write that more compactly as just list, A, B, C.
Now lists created with a list primitive are exactly like list created with the
cons. And that's a list of three elements.
If I run this now, what you'll see is that because I've turned on the list
abbreviations language, Racket shows me the value of this expression using list
notation. And it also shows me the value of this
expression using list notation. So, you can still construct lists with
cons, and in many cases you'll still want to construct them with cons.
But, you can also construct them with lists.
And Rackets always going to show them to you with list.
Now, just like cons, list is a primitive for constructing lists, and it's a
notation Racket uses for, presenting list values.
And so for example, you can say list plus one two plus three four plus five six.
So this is a call to the list [UNKNOWN] with three [UNKNOWN].
When you run that you'll get back this 3, 7, 11 because those operands have reduced
to values before the list is created and so what you see is a list with the
values. Now, an important point here, which is
that when you construct lists one at a time in a function you're still going to
want to use cons and let me show you why. Suppose I say define L1 is list b, c and
define L2 is list List d, d, e, f, now I'll run that, now I've got those two
definitions. Now watch this, if I say Cons a onto l1.
That produces the value I probably wanted.
Which was to take the list L1, and add an a to the front.
On the other hand, if I say list. A, L1, that doesn't produce the value I
want. That produces a list in which the first
element is a, and the second element is the value of L1, which is some other
list. So I get a front end list here that has a
as it's first element, and a list as it's second element.
I'm not saying you can't have lists with lists in them.
Sometimes that's exactly what you want. All I'm saying here, is watch out when
you're trying to add one element to a list, you use cons.
You don't use lists. You use list when you're trying to make a
fully formed list all at once. So for example, you might want to say,
list L1 list L2, that might be a perfectly reasonable thing to say.
And conceivably even this middle thing would be a reasonable thing to say.
But, usually when you write something that, you're going to want to use cons.
Let me show you one other list primitive that sometimes is quite useful, it's
called append. And it consumes two lists and what it
does, let me comment these things out. Append consumes two lists, and what it
does is it produces a single list by adding the two lists together.
So it's kind of like string append. It contains, it contains all the elements
of L1 followed by all the elements of L2. Now one final thing about list notation
rather event [UNKNOWN] notation. If you find list notation confusing, you
don't have to start using right away. We're going to use it so you need to be
able to read it. It is still perfectly fine for you to
write, that lists that way, instead of, instead of that way.
They both mean the same, they are both correct.
I think of most of you will soon find it more convenient to use list notation, but
Kan's notation, is still perfectly valid. There's a few exercises that come up now
to help test your understanding of a list notation.
And in particular to test the issue of when to use cons to add something to a
list.