When creating new data structures in Racket, are they usually built using cons cells or is there just a variety of ways using existing built in data structures.?
Structures!
Complicated data structures can contain values of different kinds, but cons-cells (list) are for single-linked lists primarily.
If you read SICP you will see, that they use cons-cell lists to build other kinds of data structures - but Scheme at the time did not have structurs (aka records).
So they were forced to use lists.
@soegaard2 Thanks, I’ve read some of SICP, but still plodding through.
I consider match
to be indispensable in combination with structures, so I can construct and deconstruct values using concise positional arguments. I think the Rebellion library offers a way to define records that use keyword arguments instead.
They can be pattern matched on as well.
I wonder if there’s a stylistic preference for values
vs. just a list and a pattern match. It feels weird to have this values
thing that somehow feels like data I can’t really easily destruct on.
so I always tend towards using lists.
I recommend avoiding values
unless you’re micro-optimizing
lists and pattern matching works in a pinch, but making a transparent struct is much better
so sincere question, if I’m only going to use it at (say) one callsite, why the need to codify explicitly as a struct?
or like 1–2 callsites
i realize 1 doesn’t make any sense
Arity checking and field names
ah hm, yeah, I see your point.
also if it’s more than a small number of fields (like, say, three or more maybe), it’s really helpful to have a keyword constructor for the struct type so you don’t have to remember their order.
Rebellion has tuple types which are ordered (like regular structs), and record types which aren’t (like structs but with keyword arguments). I usually default to record types and use tuple types when there’s an “obvious” order, like the x and y coordinates of a point.
@krismicinski It’s an interesting question. People wildly disagree. One point of view: “If lists aren’t part of the problem, then they shouldn’t be part of the solution.” Example: Integer division results in both a quotient and a remainder. Some like the symmetry of passing and returning multiple values. Others dislike that the semantics of function returns become more complicated.
Here is a link to an extensive discussion on multiple values: https://groups.google.com/d/msg/comp.lang.scheme/ruhDvI9utVc/786ztruIUNYJ