DOC PREVIEW
U of I CS 241 - LECTURE

This preview shows page 1-2-3-21-22-23-43-44-45 out of 45 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 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 45 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 45 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 45 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 45 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 45 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 45 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 45 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 45 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineRecordsOverviewRecord TypesRecord ValuesPattern MatchingAccess and CreationVariantsOverviewEnumerationsDisjoint UnionsPolymorphism and OptionMaps and FoldsRecursive TypesRecursive TypesFunctions over Recursive TypesMutually Recursive TypesNested Recursive TypesInfinite Recursive ValuesOutlineRecordsVariantsRecursive 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 t he 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; }◮person is the type being introduced◮name, ss and age are t he labels, or fieldsMark Hills CS421 Lecture 5: User-Defined Datatypes in OCamlOutlineRecordsVariantsRecursive TypesOverviewRecord TypesRecord ValuesPattern MatchingAccess and CreationRecord ValuesRecords buil t 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 essentiall y 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, etc)◮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


View Full Document

U of I CS 241 - LECTURE

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download LECTURE
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 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 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?