Standard ML Mini-tutorial [1mm] (in particular SML NJ)   - Programming Languages CS442
21 pages
English

Standard ML Mini-tutorial [1mm] (in particular SML NJ) - Programming Languages CS442

-

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
21 pages
English
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

Standard ML Mini tutorial(in particular SML/NJ)Programming Languages CS442David TomanSchool of Computer ScienceUniversity of WaterlooDavid Toman (University of Waterloo) Standard ML 1 / 21Introduction• SML (Standard Meta Language)⇒ originally part of the LCF project (Gordon et al.)• Industrial strength PL (SML’90, SML’97)⇒ based formal semantics (Milner et al.)• SML “Basis Library” (all you ever wanted)⇒ based on advanced module system• Quality compilers:⇒ SML/NJ (Bell Labs)⇒ Moscow MLDavid Toman (University of Waterloo) Standard ML 2 / 21Features• Everything is built from expressions⇒ functions are first class citizens⇒ pretty much extension of our simple functional PL• Support for structured values: lists, trees, . . .• Strong type system⇒ let polymorphicfunctions⇒ type inference• Powerful module system⇒ signatures, implementations, ADTs,. . .• Imperative features (e.g., I/O)David Toman (University of Waterloo) Standard ML 3 / 21Tutorial Goals1 Make link from our functional language to SML2 Provide enough SML syntax and examples for A2• How to use SML/NJ interactive environment• How to write simple functional programs• How to define new data types• How to understand compiler errors• Where to find more information3 Show type inference in action (so we understand what’s coming)David Toman (University of Waterloo) Standard ML 4 / 21Getting started• Starting it up: sml in UNIX (click somewhere in W/XP)ExampleStandard ML of New Jersey ...

Informations

Publié par
Nombre de lectures 58
Langue English

Extrait

TdivaDevsrtioymonaU(ino)StandafWaterlo
Programming Languages CS442
(in particular SML/NJ)
Standard ML Mini-tutorial
David Toman
School of Computer Science University of Waterloo
LMdr12/1
yoitrsveni(UanomTdivaD
Quality compilers: SML/NJ (Bell Labs) Moscow ML
SML (Standard Meta Language) originally part of the LCF project (Gordon et al.)
Introduction
Industrial strength PL (SML’90, SML’97) based formal semantics (Milner et al.)
SML “Basis Library” (all you ever wanted) based on advanced module system
2/21rdMLnaad)otSreolWfta
lesnoitcmolypot-uncfhirpaDivTd
type inference
Powerful module system signatures, implementations, ADTs,. . .
Imperative features (e.g., I/O)
Everything is built from expressions functions are rst class citizens pretty much extension of our simple functional PL
Strong type system
Features
Support for structured values : lists, trees, . . .
/312drLMdaanSto)loeratfWoytisrevinU(namo
tS)oolretaWfoytirsveni(UanomdTvi
3
How to use SML/NJ interactive environment How to write simple functional programs How to dene new data types How to understand compiler errors Where to nd more information
Provide enough SML syntax and examples for A2
Show type inference in action (so we understand what’s coming)
2
Tutorial Goals
1
Make link from our functional language to SML
4/21rdMLandaaD
ol)otSnaaddrLM/521
Getting started
Example -1; val it = 1 : int -2+3; val it = 5 : int -
Starting it up: sml in UNIX (click somewhere in W/XP)
Example Standard ML of New Jersey, Version 110.0.7 [CM&CMB] -
Notation and simple examples:
I type in blue , SML replies in black
great support in Emacs
ivaDU(namoTditrsvenieratfWyo
Simple Declarations
and use them:
Example -val y = x*2; val y = 20 : int
Example -val x = 2*3+4; val x = 10 : int
We can create declarations (bindings):
now x stands for 10
analogue of an environment { x = 10 y = 20 }
o)StandardML6/21versityofWaterloaDivTdmonaU(in
idavDnU(namoTytisreviterlofWatandoo)S7L2/raMd
and these types come with additional operations
Example -1.0; val it = 1.0 : real -"abc"; val it = "abc" : string -#"a"; val it = #"a" : char
Example "abc"^"def"; -val it = "abcdef" : string
1
there is more than integers:
Types of Simple Things
λ -abstractions:
Example -fn x => x+1; val it = fn : int -> int
functions can be “declared” and “used”:
Example -val twice = (fn x => 2*x); val twice = fn : int -> int -twice y; val it = 40 : int
Functions
218/MLrddanatS)oolretaWfoyrsitnivean(UdTomaDiv
what if we wanted a recursive function ?
match patterns better cover all possible parameter values!
there is a rec construction (which almost nobody uses) functions are dened “explicitly” using a fun declaration:
Example -fun fac n = if (n=0) then 1 else n*(fac (n-1)); val fac = fn : int -> int
but more commonly using match patterns :
atdnraMd9L2/1
Example -fun fac 0 = 1 = | fac n = n*(fac (n-1)); val fac = fn : int -> int -fac 10; val it = 3628800 : int
Functions
DS)oolretaWfoytiserivUnn(maToidav
real
and projections:
bool
*
Pairs and k -tuples:
Example -#3(triple); val it = 1.0 : real -val (x,y) = pair; val x = 1 : int val y = "abc" : string
*
Example -val pair = (1,"abc"); val pair = (1,"abc") : int * string -val triple = (1,true,1.0); val triple = (1,true,1.0) : int
12
Complex Types: Tuples
1L/0raMdtandoo)SterlofWavadiDn(UnTomasityiver
ndardML1rloo)Sta
Complex Types: Lists
List construction
Example
and operations:
int
-hd l; val it = 1 : int -tl l; val it = [2,3] : -tl(tl(tl l)); val it = [] : int list
list
/112
list
Example
-1::nil; val it = [1] : int list -val l = [1,2,3]; val l = [1,2,3] : int
Toidn(maavDfoytetaWvinUisre
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents