DOC PREVIEW
U of I CS 421 - Lecture 5

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineRecordsVariantsRecursive TypesCS421 Lecture 5: User-Defined Datatypes inOCaml1Mark [email protected] of Illinois at Urbana-ChampaignJune 6, 20061Based on slides by Mattox Beckman, as updated by Vikram Adve, GulAgha, and Elsa GunterMark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesRecordsVariantsRecursive TypesMark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesObjectivesUser-defined datatypes in OCaml will be heavily used in upcomingMPs and are a fundamental feature of the ML family of languages.As a result of this lecture (plus MP2), you should. . .◮understand the distinctions between different user-defineddata types◮know how to create new data types◮be able to manipulate data types with functions and patternmatchingMark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewRecord TypesRecord ValuesPattern MatchingAccess and CreationRecords◮Records serve then same programming purpose as tuples◮Provide better documentation, more readable code◮Allow components to be accessed by label instead of position◮Labels (aka field names must be unique)◮Fields accessed by suffix dot notationMark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewRecord TypesRecord ValuesPattern MatchingAccess and CreationRecord TypesRecord types must be declared before they can be used1 # type person = {name : string; ss : (int * int * int);2 age : int};;3 type person = { name : string; ss : int * int * int;4 age : int; }◮pe rson is the type being introduced◮name, ss and age are the labels, or fieldsMark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewRecord TypesRecord ValuesPattern MatchingAccess and CreationRecord ValuesRecords built with labels; order does not matter1 # let teacher = {name = "Mark Hills";2 age = 102;3 ss = (123,45,6789)};;4 val teacher : person =5 {name = "Mark Hills"; ss = (123, 45, 6789); age = 102}Mark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewRecord TypesRecord ValuesPattern MatchingAccess and CreationRecord Values1 # let student = {ss=(987,65,4321);2 name="Rebecca Hills";3 age=3};;4 val student : person =5 {name = "Rebecca Hills"; ss = (987, 65, 4321); age = 3}67 # student = teacher;;8 - : bool = falseMark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewRecord TypesRecord ValuesPattern MatchingAccess and CreationRecord Pattern Matching1 # let {name = mark; age = age; ss = (_,_,s3)} = teacher;;2 val mark : string = "Mark Hills"3 val age : int = 1024 val s3 : int = 6789Mark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewRecord TypesRecord ValuesPattern MatchingAccess and CreationRecord Field Access1 # let soc_sec = teacher.ss;;2 val soc_sec : int * int * int = (123, 45, 6789)Mark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewRecord TypesRecord ValuesPattern MatchingAccess and CreationNew Records from Old1 # let birthday person = {person with age = person.age + 1};;2 val birthday : person -> person = <fun>34 # birthday teacher;;5 - : person = {name = "Mark Hills"; ss = (123, 45, 6789);6 age = 103}Mark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewRecord TypesRecord ValuesPattern MatchingAccess and CreationNew Records from Old1 # let new_id name soc_sec person =2 { person with name = name; ss = soc_sec };;3 val new_id : string -> int * int * int ->4 person -> person = <fun>56 # new_id "John Smith" (321,54,9876) student;;7 - : person = {name = "John Smith"; ss = (321, 54, 9876);8 age = 3 }Mark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewEnumerationsDisjoint UnionsPolymorphism and OptionMaps and FoldsVariants◮Variants, or variant records, provide for a single datatype withmultiple forms◮Gives us a way to represent different types of data with onetype nameCommon examples:◮lists◮trees◮syntactic elements in a programming languageMark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewEnumerationsDisjoint UnionsPolymorphism and OptionMaps and FoldsVariant SyntaxVariants are declared as types with constructors, which take zeroor more data items; constructor names always start with caps◮no data with constructor name, constructor is a constant◮data types can include polymorphic types (more on this later)◮each constructor essentially a function from constructor’s datato variant typefun x1· · · xn-> C x1· · · xn(type t1· · · tn-> name)◮constructors the basis of almost all pattern matchingMark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewEnumerationsDisjoint UnionsPolymorphism and OptionMaps and FoldsVariant SyntaxGeneral syntax:type name= C1(* Constant *)C2of ty2(* Single data item *)C3of ty3∗ · · · ∗ tyn(* Multiple data items *)Mark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewEnumerationsDisjoint UnionsPolymorphism and OptionMaps and FoldsEnumerations◮Enumerations are variants where all constructors are constants◮Essentially forms a collection of distinct elements (days of theweek, months of the year, et c)◮Unlike in C, enumerations in OCaml are unorderedMark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewEnumerationsDisjoint UnionsPolymorphism and OptionMaps and FoldsEnumerations: An Example1 # type weekday = Monday | Tuesday | Wednesday2 | Thursday | Friday | Saturday | Sunday;;3 type weekday =4 Monday5 | Tuesday6 | Wednesday7 | Thursday8 | Friday9 | Saturday10 | SundayMark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewEnumerationsDisjoint UnionsPolymorphism and OptionMaps and FoldsFunctions over Enumerations1 # let day_after day = match day with2 Monday -> Tuesday3 | Tuesday -> Wednesday4 | Wednesday -> Thursday5 | Thursday -> Friday6 | Friday -> Saturday7 | Saturday -> Sunday8 | Sunday -> Monday;;9 val day_after : weekday -> weekday = <fun>Mark Hills CS421 Lecture 5: User-Defined Datatypes in


View Full Document

U of I CS 421 - Lecture 5

Documents in this Course
Lecture 2

Lecture 2

12 pages

Exams

Exams

20 pages

Lecture

Lecture

32 pages

Lecture

Lecture

21 pages

Lecture

Lecture

15 pages

Lecture

Lecture

4 pages

Lecture

Lecture

68 pages

Lecture

Lecture

68 pages

Lecture

Lecture

84 pages

s

s

32 pages

Parsing

Parsing

52 pages

Lecture 2

Lecture 2

45 pages

Midterm

Midterm

13 pages

LECTURE

LECTURE

10 pages

Lecture

Lecture

5 pages

Lecture

Lecture

39 pages

Load more
Download Lecture 5
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 5 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 5 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?