/
CSE 341 Section  2 Autumn CSE 341 Section  2 Autumn

CSE 341 Section 2 Autumn - PowerPoint Presentation

trinity
trinity . @trinity
Follow
68 views
Uploaded On 2023-11-15

CSE 341 Section 2 Autumn - PPT Presentation

2017 Adapted from slides by Nick Mooney Nicholas Shahan Patrick Larson and Dan Grossman Todays Agenda Type synonyms T ype generality Equality types Syntactic sugar Type Synonyms ID: 1031756

type list string int list type int string general equality case typesthe generality consistently rank suit listbut syntactic sugar

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CSE 341 Section 2 Autumn" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. CSE 341Section 2Autumn 2017Adapted from slides by Nick Mooney, Nicholas Shahan, Patrick Larson, and Dan Grossman

2. Today’s AgendaType synonymsType generalityEquality typesSyntactic sugar

3. Type SynonymsWhat does int * int * int represent?In HW1 we called it a dateWouldn’t it be nice to reflect this representation in the source code itself?type date = int * int * int

4. type vs datatypedatatype introduces a new type name, distinct from all existing typestype is just another namedatatype suit = Club | Diamond | Heart | Spadedatatype rank = Jack | Queen | King | Ace | Num of inttype card = suit * rank

5. Type SynonymsWhy? For now, just for convenienceIt doesn’t let us do anything newLater in the course we will see another use related to modularity.

6. Type GeneralityWrite a function that appends two string lists…

7. Type GeneralityWe would expectstring list * string list -> string list‘a list * ‘a list -> ‘a listBut the type checker foundWhy is this OK?

8. More General TypesThe type‘a list * ‘a list -> ‘a liststring list * string list -> string list is more general than the type and “can be used” as any less general type, such asint list * int list -> int listBut it is not more general than the typeint list * string list -> int list

9. The Type Generality RuleThe “more general” rule A type t1 is more general than the type t2 if you can take t1, replace its type variables consistently, and get t2What does consistently mean?

10. Equality TypesWrite a list contains function…

11. Equality TypesThe double quoted variable arises from use of the = operatorWe can use = on most types like int, bool, string, tuples (that contain only “equality types”)Functions and real are not ”equality types”Generality rules work the same, except substitution must be some type which can be compared with =You can ignore warnings about “calling polyEqual”

12. Syntactic SugarIf-then-else is implemented as syntactic sugar for a case statement

13. If-then-elseWe’ve just covered case statementsHow could we implement if-then-else?case x of true => “apple” | false => “banana”if x then “apple” else “banana”

14. Adventures in pattern matchingShape exampleFunction-pattern syntax if we get to it