From 02bc5183512e1548bfb3a54bacc85443dc33f86d Mon Sep 17 00:00:00 2001 From: James Baker Date: Sun, 26 Apr 2015 09:59:15 +0100 Subject: Standard ML has references Standard ML is not a pure language and has imperative structures like references while and seq --- standard-ml.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index b545f3e1..cc5132f1 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -8,8 +8,8 @@ contributors: Standard ML is a functional programming language with type inference and some side-effects. Some of the hard parts of learning Standard ML are: Recursion, pattern matching, type inference (guessing the right types but never allowing -implicit type conversion). If you have an imperative background, not being able -to update variables can feel severely inhibiting. +implicit type conversion). Standard ML is distinguished from Haskell by including +references, allowing variables to be updated. ```ocaml (* Comments in Standard ML begin with (* and end with *). Comments can be -- cgit v1.2.3 From f8260574d9a29d5302dccf8fb883d5e3d60592d0 Mon Sep 17 00:00:00 2001 From: James Baker Date: Sun, 26 Apr 2015 10:00:21 +0100 Subject: Add examples of imperative-style control Make reference and update, add use of a while and use of seq --- standard-ml.html.markdown | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index cc5132f1..8fc849cb 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -383,6 +383,25 @@ val test_poem = readPoem "roses.txt" (* gives [ "Roses are red,", "Violets are blue.", "I have a gun.", "Get in the van." ] *) + +(* We can create references to data which can be updated *) +val counter = ref 0 (* Produce a reference with the ref function *) + +(* Assign to a reference with the assignment operator *) +fun set_five reference = reference := 5 + +(* Read a reference with the dereference operator *) +fun equals_five reference = !reference = 5 + +(* We can use while loops for when recursion is messy *) +fun decrement_to_zero r = if !r < 0 + then r := 0 + else while !r >= 0 do r := !r - 1 + +(* This returns the unit value (in practical terms, nothing, a 0-tuple) *) + +(* To allow returning a value, we can use the semicolon to sequence evaluations *) +fun decrement_ret x y = (x := !x - 1; y) ``` ## Further learning -- cgit v1.2.3 From 183ca56459828f567457337204c7ef10351a31f0 Mon Sep 17 00:00:00 2001 From: James Baker Date: Sun, 26 Apr 2015 10:01:36 +0100 Subject: Add myself as a contributor --- standard-ml.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index 8fc849cb..07896beb 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -3,6 +3,7 @@ language: "Standard ML" contributors: - ["Simon Shine", "http://shine.eu.org/"] - ["David Pedersen", "http://lonelyproton.com/"] + - ["James Baker", "http://www.jbaker.io/"] --- Standard ML is a functional programming language with type inference and some -- cgit v1.2.3