1/26
20/09/26
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
what are cond expressions?
referred to as a multi-armed conditional, we use them when we want to make an expression that has a different behaviour depending on the answer to predicates, but there is more than two options
they look like this:
(usually theres a define statement here...)
(cond [Q A]
[Q A]
[else A])
(cond [(> (image-height img) (image-width img)) "tall"]
[(> (image-width img) (image-height img)) "width"]
[else "square"])cond expression evaluation rules
cond expressions are evaluated all on the same sort of level, so while the first statement may be false, its not going to just return a false and quit. each question however must produce a boolean, which makes it move on to the next.
basically, if the first question is not a value, it should be a evaluated and replaced with its value, i.e. replace the entire cond expression with the exact same thing, but have the first Q/A pair as just false
if the first question is true or an else expression, replace the entire cond expression with the first answer.
if its false, replace the expression with a cond that doesn’t have the first Q/A pair, and start over.
an error will be thrown if there is no Q/A pairs in the expression, or if a boolean isn’t produced by the questions

what is the problem domain, and how does it influence our data?
its like the real-world situation our program pertains to. a traffic-directing program would pertain to red/yellow/green lights, a bunch of different signs, road lanes, etc., but programs don’t have these concepts within the,, so we have to represent these concepts through our structure of data in our programs
how do we move between the problem domain and our data structure mentally?
we represent information from the problem domain as data in the program
and we interpret data from the program as information in the problem domain
what is a data definition?
a comment within your program that describes how to form data of a new type, how to represent information as data, how to interpret data as information, and a template for operating on this data.
they also simplify your later functions, because you’ve established what the function can take if its signature says it takes a TLColor
(@htdd TLColor)
;; TLColor is one of:
;; - 0
;; - 1
;; - 2
;; interp. color of a traffic light - 0 is red, 1 yellow, 2 green
(define (fn-for-tlcolor c)
(cond [(= c 0) (...)]
[(= c 1) (...)]
[(= c 2) (...)]))
the first line establishes the new type’s name, and what pieces of data consist of this thing (i.e. its one of xyz…)
the next lines are the possible option for this new type
the interp. line tells you what the hell these things mean to the problem domain
and then you write a template function to show how one could operate on these types of data
how to build a data definition (as found in the HtDD recipe)
the first step is to identify the inherent structure of the information
after, a data definition consists of an @htdd tag followed by four or five elements:
a possible structure definition (NOT UNTIL COMPOUND DATA)
a type comment that defines a new type name and describes how to form data of that type
an interpretation that describes the correspondence b/w information and data
one or more examples of the data
a template for a 1 argument function operating on data of this type
what is so important about the inherent structure of the information?
the structure of the information in the program's domain determines the kind of data definition used,
which in turn determines the structure of the data-driven templates and helps determine the function examples (check-expects),
and therefore the structure of much of the final program design.
forms of data and what kind of definition to use:
atomic
use a simple atomic data definition
forms of data and what kind of definition to use:
numbers within a certain range
use an interval definition
forms of data and what kind of definition to use:
a fixed number of distinct items
use an enumeration definition
forms of data and what kind of definition to use:
2 or more subclasses, at least one of which is not a distinct item
use an itemization definition
forms of data and what kind of definition to use:
two or more items that naturally belong together
use a compound data definition
forms of data and what kind of definition to use:
data naturally composed of different parts
use a "references to other defined type” data definition
forms of data and what kind of definition to use:
data is of arbitrary (unknown) size
use a self-referential or mutually referential data definition
what does the how to design data recipe tell us?
what kinds of data to use in order to properly represent your problem domain, and how to build a data definition
what does it mean for data to be atomic
it means that the data cannot be taken apart into pieces that are meaningfully part of the same problem domain.
for example, the city name “Vancouver” would be important to a program pertaining to a map or list of cities, but breaking it down into “V” would be useless to my program, and therefore the term “Vancouver” is atomic data
what does a simple atomic data definition look like?
(@htdd Time)
;; Time is Natural
;; interp. number of clock ticks since start of game
(define START-TIME 0)
(define OLD-TIME 1000)
(@dd-template-rules atomic-non-distinct) ;Natural
(define (fn-for-time t)
(... t))
;; Template rules used:
;; - atomic non-dstinct: Naturalwe start with the tag to show which recipe we’re using
then we have a type comment that establishes the new type (Time) and the atomic data’s underlying primitive type (like String, Boolean, in this case, Natural)
then there’s the interpretation comment, which describes what this data means to the problem domain
then we have examples of the data. for simple atomic data, this can literally be just defining a constant
the we make a template for this data definition (found on Data Driven Templates) naming the template after the TypeName helps us read it later.
they may ask you to include template rules used, you get these from the recipe page, and then you list the type of primitive data the new Type is
and then you comment out the template.
what is the recipe for Data Driven Templates
for a given type TypeName the data driven template is:
(define (fn-for-type-name x)
<body>)capitalization of data types
we use CamelCase. ex: CityName, ButtFuck, PussyFart
brackets and integer interval boundaries
square brackets; [x, y] are inclusive of the ending numbers
round brackets; (x, y) are exclusive of the ending numbers
its like math
interval data definition changes
In your type comment, your data type will likely be Number, Natural, or Integer (if you’re allowing decimals) and then the interval of allowed values (remember your bracket choices)
then your interpretation comment
then your examples, typically one for each border number in the interval, and one in the middle
then your template function, which is pretty basic, just (… <lowercase of your TypeName>), also check the HtDD page too
then ;; Template rules used:
;; - <what classification of data it was, typically atomic-non-distinct>: <then the primitive type you listed above>[the range you gave earlier]
ex:
(@htdd SeatNum)
;; SeatNum is Natural[1, 32]
;; interp. seat numbers in a row
(define SN1 1) ;aisle
(define SN2 12) ;middle
(define SN3 32) ;aisle
(define (fn-for-seat-num sn)
(... sn))
;; Template rules used:
;; - atomic-non-distinct: Natural[1, 32]how to do enumeration data definitions:
(@htdd LetterGrade)
;; LetterGrade is one of:
;; - "A"
;; - "B"
;; - "C"
;; interp. these are the grades students can have in the class
(@dd-template-rules one-of ;3 cases
atomic-distinct ;"A"
atomic-distinct ;"B"
atomic-distinct) ;"C"
(define (fn-for-letter-grade lg)
(cond [(string=? lg "A") (...)]
[(string=? lg "B") (...)]
[(string=? lg "C") (...)]))
your type comment will actually include the “one of:” statement this time because you have a set number of subclasses under it
Then you go on to list each subclass under it
then your interp. statement
then: <examples are redundant for enumerations>
then your template rules section, which follows the one-of section, and then a list of the primitive type of what each subclass is, as well as comments denoting how many subclasses there are, and what primitive classification corresponds to each subclass
then you write your actual template, and it is just a cond statement with a question for each subclass in the enumeration. you don’t have to do an else statement. and when writing your cond statement, consult the table once again to reference the subclass and its primitive type to determine the question and cond answer
what is the case to use an enumeration?
when the domain problem consists of two or more distinct values
and an enumeration type comment has one of followed by the distinct value subclasses
whats the difference between an enumeration and an itemization?
enumerations and itemizations both have one of statements, but enumeration subclasses will all be distinct, where itemizations don’t always have that
itemization scenario rerun
for when the domain information is comprised of 2 or more subclasses, where at least one of which is not a distinct data item.
what does an itemization data definition look like?
(@htdd CountStatus)
;; CountDown is one of:
;; - false
;; - Natural[1, 10]
;; - "complete"
;; interp. false = contdown not started, Natural[1, 10] means running + seconds left "complete" means its over.
(@dd-template-rules one-of ;3 cases
atomic-distinct ;false
atomic-non-distinct ;Natural[1, 10]
atomic-distinct) ;"complete"
(define CD1 false)
(define CD2 10) ;just started running
(define CD3 1) ;almost over
(define CD4 "complete")
(define (fn-for-countdown c)
(cond [(false? c) (...)]
[(number? c) (... c)]
[else (...)]))this time, we need the simple examples, where enumerations dont.
note, you may need extra guards and an and statement to ensure that the number given is part of the interval, but only if theres multiple intervals where n incorrect variable could pass the “number? x” guard.
note, this multi-guard rule also applies to itemizations that contain more that one subclass of the same primitive type. if you had three different strings in one, then only a (string=? str) on each part of the cond for it, then the wrong strings would be able to pass for it. so add another guard, and possibly even remove the (string=? str) guard other subsequent string questions
how many tests should enumeration data definitions have?
functions built off of this definition should have at least as many test cases as subclasses in the enumeration