From 9999a30e045f59fe3347b8822f71fff32b3ffc27 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Thu, 4 Jan 2018 15:49:43 -0600 Subject: Add `case` expression to SML docs Show an example of pattern-matching using the case expression. --- standard-ml.html.markdown | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index 5db15b5c..b34f1c08 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -6,6 +6,7 @@ contributors: - ["David Pedersen", "http://lonelyproton.com/"] - ["James Baker", "http://www.jbaker.io/"] - ["Leo Zovic", "http://langnostic.inaimathi.ca/"] + - ["Chris Wilson", "http://sencjw.com/"] --- Standard ML is a functional programming language with type inference and some @@ -266,6 +267,16 @@ fun second_elem (x::y::xs) = y fun evenly_positioned_elems (odd::even::xs) = even::evenly_positioned_elems xs | evenly_positioned_elems [odd] = [] (* Base case: throw away *) | evenly_positioned_elems [] = [] (* Base case *) + +(* The case expression can also be used to pattern match and return a value *) +datatype temp = + C of real + | F of real + +fun temp_to_f t = + case t of + C x => x * (9.0 / 5.0) + 32.0 + | F x => x (* When matching on records, you must use their slot names, and you must bind every slot in a record. The order of the slots doesn't matter though. *) -- cgit v1.2.3 From c4664b31e5231fb56cb36cbebcef50ee699fda07 Mon Sep 17 00:00:00 2001 From: Chariton Charitonidis Date: Fri, 15 Mar 2019 11:01:23 +0200 Subject: Added a comment to better demonstrate custom datatypes. It is not obvious for a beginner (like me) to declare a new value of the temp datatype which can be either C (celsius) or F (fahrenheit). I think it would be better to demonstrate the declaration of such a datatype. --- standard-ml.html.markdown | 3 +++ 1 file changed, 3 insertions(+) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index b34f1c08..0ba42f39 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -272,6 +272,9 @@ fun evenly_positioned_elems (odd::even::xs) = even::evenly_positioned_elems xs datatype temp = C of real | F of real + +(* Declaring a new C temp value... + val t: temp = C 45.0 *) fun temp_to_f t = case t of -- cgit v1.2.3