mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-13 18:24:39 +02:00
Merge pull request #3037 from twopoint718/master
[Standard ML/en] Add case expression to SML docs
This commit is contained in:
@@ -6,6 +6,7 @@ contributors:
|
|||||||
- ["David Pedersen", "http://lonelyproton.com/"]
|
- ["David Pedersen", "http://lonelyproton.com/"]
|
||||||
- ["James Baker", "http://www.jbaker.io/"]
|
- ["James Baker", "http://www.jbaker.io/"]
|
||||||
- ["Leo Zovic", "http://langnostic.inaimathi.ca/"]
|
- ["Leo Zovic", "http://langnostic.inaimathi.ca/"]
|
||||||
|
- ["Chris Wilson", "http://sencjw.com/"]
|
||||||
---
|
---
|
||||||
|
|
||||||
Standard ML is a functional programming language with type inference and some
|
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
|
fun evenly_positioned_elems (odd::even::xs) = even::evenly_positioned_elems xs
|
||||||
| evenly_positioned_elems [odd] = [] (* Base case: throw away *)
|
| evenly_positioned_elems [odd] = [] (* Base case: throw away *)
|
||||||
| evenly_positioned_elems [] = [] (* Base case *)
|
| 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
|
(* 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. *)
|
every slot in a record. The order of the slots doesn't matter though. *)
|
||||||
|
Reference in New Issue
Block a user