• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/10

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

10 Cards in this Set

  • Front
  • Back

base types

Programming languages have base types, like int, bool, and unit , char

compound types

compound types, which are types that contain other types in their definition. We have already seen ways to make compound types in ML, namely by using tuple types, list types, and option types.

"each of" compound type

“Each-of”: A compound type t describes values that contain each of values of type t1, t2, ..., and tn.



Each-of types are the most familiar to most programmers. Tuples are an example: int * bool describes values that contain anint and a bool. A Java class with fields is also an each-of sort of thing.

"one of" compound type

“One-of”: A compound type t describes values that contain a value of one of the types t1, t2, ..., or tn.



One-of types are also very common but unfortunately are not emphasized as much in many introductory programming courses.int option is a simple example: A value of this type contains an int or it does not. For a type that contains an int or a bool in ML, we need datatype bindings, which are the main focus of this section of the course. In object-oriented languages with classes like Java, one-of types are achieved with subclassing, but that is a topic for much later in the course.

"self reference" compound type

Self-reference”: A compound type t may refer to itself in its definition in order to describe recursive data structures like lists and trees.



Self-reference allows types to describe recursive data structures. This is useful in combination with each-of and one-of types. For example, int list describes values that either contain nothing or contain an int and another int list. A list of integers in any programming language would be described in terms of or, and, and self-reference because that is what it means to be a list of integers.

record types

Record types are “each-of” types where each component is a named field. For example, the type {foo : int, bar : int*bool, baz : bool*int} describes records with three fields named foo, bar, and baz. This is just a new sort of type, just like tuple types were new when we learned them.

record expression (syntax, type-checking, evaluation)

In ML, we do not have to declare that we want a record type with particular field names and field types — we just write down a record expression and the type-checker gives it the right type. The type-checking rules for record expressions are not surprising: Type-check each expression to get some type ti and then build the record type that has all the right fields with the right types.Because the order of field names never matters, the REPL always alphabetizes them when printing just for consistency.



The evaluation rules for record expressions are analogous: Evaluate each expression to a value and create the corresponding record value.

"by name" access

Records and tuples are very similar. They are both “each-of” constructs that allow any number of com- ponents. The only real difference is that records are “by name” and tuples are “by position.”



This means with records we build them and access their pieces by using field names, so the order we write the fields in a record expression does not matter.

"by position" access

Records and tuples are very similar. They are both “each-of” constructs that allow any number of com- ponents. The only real difference is that records are “by name” and tuples are “by position.”



tuples do not have field names, so we use the position (first, second, third, ...) to distinguish the components.



syntactic sugar


We say, “tuples are just syntactic sugar for records with fields named 1, 2, ..., n.” It is syntactic because we can describe everything about tuples in terms of equivalent record syntax. It issugar because it makes the language sweeter. The term syntactic sugar is widely used. Syntactic sugar is a great way to keep the key ideas in a programming-language small (making it easier to implement) while giving programmers convenient ways to write things. Indeed, in Homework 1 we used tuples without knowing records existed even though tuples are records.