[racket] Confused between strings and symbols
First, don't get hung up on that single-quote that you see when the
value of that expression is printed. Depending on your DrRacket
preferences, the single-quote might not even be printed (it isn't for
me). There are 3 different ways that different programs print Racket
lists, and each way is slightly confusing.
The expression (list 'this '(is silly)) indeed has a value of a list of
2 elements, with the second element being a different list of 2 elements.
The value is equivalent to that of the expression (list 'this (list 'is
'silly)) . In the original expression, the single quote before the
parenthesis is a convenience, which which will very quickly become
intuitive to you after you are a little more familiar with lists. Don't
worry about it until you're comfortable with lists.
Three things to play around with for getting a better intuition for
pairs and lists early on:
* Try drawing a pair diagram
("http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-15.html") of the
above list, and see if that gives you a more concrete understanding of
how the "car" and "cdr" of each pair is used in the representation.
* In your DrRacket "Interactions" pane, experiment with constructing
lists different ways using "list" and "cons", and see how each prints.
For example, using only "cons", construct the lists that print like '(a
. b) , '(a b) , '(a b . c) , '(a b c) , '(a (b c)) , etc.
* In DrRacket, define a list, and then write some expressions that get
elements from the list or navigate nested lists, using "pair?",
"symbol?", "car", "cdr", "length", and perhaps "map". For example, on
your original list, what expression gives you the length for the second
list?
Extra credit: Using the DrRacket "Definitions" pane, construct a tree of
animal taxonomy using a nested list representation, using the "list"
procedure, and using symbols for the names. Include at least 20
different names. Big Hint: Use a list for each name, with the first
element of the list being the parent, such as reptile, and the other
elements being lists representing immediate children of reptile in your
taxonomy, in no particular order. The result should be simple; if we
showed you the answer, you'd understand it quickly, but you might
understand it better if you had to work through it yourself. Remember
to use line breaks in your code, and let DrRacket auto-indent of the
lines of your code expose the tree structure visually.
Double Extra Credit: Define the same tree again, only using the list
literal syntax we avoided before (the single-quote in front of the
parenthesis), and see how much smaller and more readable it is.
Triple Extra Credit: If you've learned some recursion, write a procedure
to traverse the tree and "printf" a set of statements based on
parent-child relationships in the tree, like "a alligator is a reptile".
Neil V.
Bo Gus wrote at 10/27/2013 04:23 PM:
> If I evaluate this:
>
> (list 'this '(is silly))
>
> I get this:
>
> '(this (is silly))
>
> I just want to check I understand what is happening here.
>
>
> list always returns a proper list hence I see no dotted notation here.
>
> The return value is quoted which means it is a symbol? But there are
> spaces so can it be a symbol? And in addition, list returns a proper
> list so it is a list? A symbolic list?
>
> Or does the quote in returned just mean it is string data in the list?
>
> The nested parentheses around is silly is indicating that this is a
> inner list - a list within a list? What is in the inner list? Two
> strings? One string made up of the characters i, s, space, s, i, l,
> l, y ?
>
> I suppose the thing that confuses me the most is the idea of a symbol.
> There are no quotes around the textual information in the returned
> data so do I assume the text is not string data? If so are they symbols?
>
> Please help clarify.