First Look at ML - Modern Programming Languages - Lecture Slides, Slides of Advanced Computer Programming

The key points are: First Look at Ml, Meta Language, Popular Functional Languages, Robin Milner’s Group, Number of Dialects, Special Variable, Defining Variables, Tuples and Lists, Defining Functions, Type Annotations

Typology: Slides

2012/2013

Uploaded on 04/18/2013

palvani
palvani 🇮🇳

4.5

(2)

83 documents

1 / 56

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A First Look at ML
Chapter Five Modern Programming Languages, 2nd ed. 1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38

Partial preview of the text

Download First Look at ML - Modern Programming Languages - Lecture Slides and more Slides Advanced Computer Programming in PDF only on Docsity!

A First Look at ML

Chapter Five Modern Programming Languages, 2nd ed. (^) Docsity.com 1

ML

 Meta Language

 One of the more popular functional

languages (which, admittedly, isn’t saying

much)

 Edinburgh, 1974, Robin Milner’s group

 There are a number of dialects

 We are using Standard ML, but we will just

call it ML from now on

Chapter Five Modern Programming Languages, 2nd ed. (^) Docsity.com 2

Outline

 Constants

 Operators

 Defining Variables

 Tuples and Lists

 Defining Functions

 ML Types and Type Annotations

Chapter Five Modern Programming Languages, 2nd ed. (^) Docsity.com 4

Chapter Five Modern Programming Languages, 2nd ed. 5

val it = 1234 : int

  • 123.4; val it = 123.4 : real

Integer constants: standard decimal , but use tilde for unary negation (like ~1 )

Real constants: standard decimal notation Note the type names: int , real

Chapter Five Modern Programming Languages, 2nd ed. 7

  • "fred"; val it = "fred" : string
  • "H"; val it = "H" : string
  • #"H"; val it = #"H" : char

String constants: text inside double quotes Can use C-style escapes: \n , \t , \ , " , etc.

Character constants: put # before a 1-character string Note type names: string and char

Outline

 Constants

 Operators

 Defining Variables

 Tuples and Lists

 Defining Functions

 ML Types and Type Annotations

Chapter Five Modern Programming Languages, 2nd ed. (^) Docsity.com 8

Chapter Five Modern Programming Languages, 2nd ed. 10

  • "bibity" ^ "bobity" ^ "boo"; val it = "bibitybobityboo" : string
  • 2 < 3; val it = true : bool
  • 1.0 <= 1.0; val it = true : bool
  • #"d" > #"c"; val it = true : bool
  • "abce" >= "abd"; val it = false : bool

String concatenation: ^ operator Ordering comparisons: < , > , <= , >= , apply to string , char , int and real

Order on strings and characters is lexicographic

Chapter Five Modern Programming Languages, 2nd ed. 11

val it = false : bool

  • true <> false; val it = true : bool
  • 1.3 = 1.3; Error: operator and operand don't agree [equality type required] operator domain: ''Z * ''Z operand: real * real in expression: 1.3 = 1.

Equality comparisons: = and <>

Most types are equality testable: these are equality types Type real is not an equality type

Chapter Five Modern Programming Languages, 2nd ed. 13

  • true orelse 1 div 0 = 0; val it = true : bool

Note: andalso and orelse are short-circuiting operators: if the first operand of orelse is true, the second is not evaluated; likewise if the first operand of andalso is false

Technically, they are not ML operators, but keywords All true ML operators evaluate all operands

Chapter Five Modern Programming Languages, 2nd ed. 14

  • if 1 < 2 then #"x" else #"y"; val it = #"x" : char
  • if 1 > 2 then 34 else 56; val it = 56 : int
  • (if 1 < 2 then 34 else 56) + 1; val it = 35 : int

Conditional expression (not statement) using ifthenelse … Similar to C's ternary operator: (1<2)? 'x' : 'y' Value of the expression is the value of the then part, if the test part is true, or the value of the else part otherwise There is no ifthen construct

Chapter Five Modern Programming Languages, 2nd ed. 16

val it = 2 : int

  • 1.0 * 2.0; val it = 2.0 : real
  • 1.0 * 2; Error: operator and operand don't agree [literal] operator domain: real * real operand: real * int in expression: 1.0 * 2

The ***** operator, and others like + and < , are overloaded to have one meaning on pairs of integers, and another on pairs of reals ML does not perform implicit type conversion

Chapter Five Modern Programming Languages, 2nd ed. 17

  • real(123); val it = 123.0 : real
  • floor(3.6); val it = 3 : int
  • floor 3.6; val it = 3 : int
  • str #"a"; val it = "a" : string Builtin conversion functions: real ( int to real ), floor ( real to int ), ceil ( real to int ), round ( real to int ), trunc ( real to int ), ord ( char to int ), chr ( int to char ), str ( char to string )

You apply a function to an argument in ML just by putting the function next to the argument. Parentheses around the argument are rarely necessary, and the usual ML style is to omit them

Chapter Five Modern Programming Languages, 2nd ed. 19

  • square 2+1; val it = 5 : int
  • square (2+1); val it = 9 : int

Function application has higher precedence than any operator Be careful!

Practice

Chapter Five Modern Programming Languages, 2nd ed. 20

What if anything is wrong with each of these expressions? trunc 5 ord "a" if 0 then 1 else 2 if true then 1 else 2. chr(trunc(97.0)) chr(trunc 97.0) chr trunc 97.