If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Saturday, October 12, 2013

Datatypes in ML

datatype mytype =
TwoInts of int * int
|Str of string
|Pizza
fun f(x: mytype) =
case x of
Pizza => 3
|Str s => 8
|TwoInts(i1,i2) => i1 + i2
val a = Str "hi"
val b = Pizza
val c = TwoInts(2,3)
val d = f c
val e = f Pizza
(* output of program
use "datatypes.sml";
[opening datatypes.sml]
datatype mytype = Pizza | Str of string | TwoInts of int * int
val f = fn : mytype -> int
val a = Str "hi" : mytype
val b = Pizza : mytype
val c = TwoInts (2,3) : mytype
val d = 5 : int
val e = 3 : int
val it = () : unit
*)
view raw gistfile1.sml hosted with ❤ by GitHub

No comments:

Post a Comment