11 - Click to edit Master subtitle style Chapter Eleven...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Chapter Eleven Modern Programming Languages, 2nd ed. A Fourth Look At ML Chapter Eleven Modern Programming Languages, 2nd ed. 11 Chapter Eleven Modern Programming Languages, 2nd ed. Type Definitions Predefined, but not primitive in ML: Type constructor for lists: Defined for ML in ML Chapter Eleven Modern Programming Languages, 2nd ed. 22 datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list Chapter Eleven Modern Programming Languages, 2nd ed. Outline Enumerations Data constructors with parameters Type constructors with parameters Recursively defined type constructors Farewell to ML Chapter Eleven Modern Programming Languages, 2nd ed. 33 Chapter Eleven Modern Programming Languages, 2nd ed. Defining Your Own Types New types can be defined using the keyword datatype These declarations define both: type constructors for making new (possibly polymorphic) types data constructors for making values of those new types Chapter Eleven Modern Programming Languages, 2nd ed. 44 Chapter Eleven Modern Programming Languages, 2nd ed. Example day is the new type constructor and Mon , Tue , etc. are the new data constructors Why constructors? In a moment we will see how both can have parameters Chapter Eleven Modern Programming Languages, 2nd ed. 55- datatype day = Mon | Tue | Wed | Thu | Fri | Sat | Sun; datatype day = Fri | Mon | Sat | Sun | Thu | Tue | Wed- fun isWeekDay x = not (x = Sat orelse x = Sun); val isWeekDay = fn : day -> bool- isWeekDay Mon; val it = true : bool- isWeekDay Sat; val it = false : bool Chapter Eleven Modern Programming Languages, 2nd ed. No Parameters The type constructor day takes no parameters: it is not polymorphic, there is only one day type The data constructors Mon , Tue , etc. take no parameters: they are constant values of the day type Capitalize the names of data constructors Chapter Eleven Modern Programming Languages, 2nd ed. 66- datatype day = Mon | Tue | Wed | Thu | Fri | Sat | Sun; datatype day = Fri | Mon | Sat | Sun | Thu | Tue | Wed Chapter Eleven Modern Programming Languages, 2nd ed. Strict Typing ML is strict about these new types, just as you would expect Unlike C enum , no implementation details are exposed to the programmer Chapter Eleven Modern Programming Languages, 2nd ed. 77- datatype flip = Heads | Tails; datatype flip = Heads | Tails- fun isHeads x = (x = Heads); val isHeads = fn : flip -> bool- isHeads Tails; val it = false : bool- isHeads Mon; Error: operator and operand don't agree [tycon mismatch] operator domain: flip operand: day Chapter Eleven Modern Programming Languages, 2nd ed....
View Full Document

Page1 / 43

11 - Click to edit Master subtitle style Chapter Eleven...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online