From d8a3184a73cfbb3be3ab99e7d7e3ebc577d06494 Mon Sep 17 00:00:00 2001 From: Yu Zhang <583181285@qq.com> Date: Fri, 10 Feb 2017 01:54:31 -0600 Subject: [standard-ml/en-en] Format fixing and some APIs (#2645) * [standard-ml/en-en] Format fixing * [standard-ml/en-en] Add descriptions for `map` and `foldl` * [standard-ml/en-en] Add myself as a contributor * review --- standard-ml.html.markdown | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index 133e4f54..c9eb2a2e 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -235,17 +235,18 @@ val hmm = answer "What is the meaning of life, the universe and everything?" (* Functions can take several arguments by taking one tuples as argument: *) fun solve2 (a : real, b : real, c : real) = - ( (~b + Math.sqrt(b * b - 4.0*a*c)) / (2.0 * a), - (~b - Math.sqrt(b * b - 4.0*a*c)) / (2.0 * a) ) + ((~b + Math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a), + (~b - Math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a)) (* Sometimes, the same computation is carried out several times. It makes sense to save and re-use the result the first time. We can use "let-bindings": *) fun solve2 (a : real, b : real, c : real) = - let val discr = b * b - 4.0*a*c + let val discr = b * b - 4.0 * a * c val sqr = Math.sqrt discr val denom = 2.0 * a in ((~b + sqr) / denom, - (~b - sqr) / denom) end + (~b - sqr) / denom) + end (* Pattern matching is a funky part of functional programming. It is an @@ -292,6 +293,9 @@ val thermometer = val some_result = (fn x => thermometer (x - 5) ^ thermometer (x + 5)) 37 (* Here is a higher-order function that works on lists (a list combinator) *) +(* map f l + applies f to each element of l from left to right, + returning the list of results. *) val readings = [ 34, 39, 37, 38, 35, 36, 37, 37, 37 ] (* first an int list *) val opinions = List.map thermometer readings (* gives [ "Cold", "Warm", ... ] *) @@ -324,7 +328,11 @@ val n = op + (5, 5) (* n is now 10 *) (* 'op' is useful when combined with high order functions because they expect functions and not operators as arguments. Most operators are really just infix functions. *) -val sum_of_numbers = foldl op+ 0 [1,2,3,4,5] +(* foldl f init [x1, x2, ..., xn] + returns + f(xn, ...f(x2, f(x1, init))...) + or init if the list is empty. *) +val sum_of_numbers = foldl op+ 0 [1, 2, 3, 4, 5] (* Datatypes are useful for creating both simple and complex structures *) @@ -407,7 +415,8 @@ fun writePoem(filename) = let val file = TextIO.openOut(filename) val _ = TextIO.output(file, "Roses are red,\nViolets are blue.\n") val _ = TextIO.output(file, "I have a gun.\nGet in the van.\n") - in TextIO.closeOut(file) end + in TextIO.closeOut(file) + end (* Read a nice poem from a file into a list of strings *) fun readPoem(filename) = -- cgit v1.2.3 From add335bd0bc51313dd6c86578c275898b69b6b55 Mon Sep 17 00:00:00 2001 From: Kartik Singhal Date: Tue, 8 Aug 2017 17:19:21 -0700 Subject: Redundancy is not permitted in pattern matching --- standard-ml.html.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index c9eb2a2e..22086699 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -351,7 +351,10 @@ val _ = print (say(Red) ^ "\n") fun say Red = "You are red!" | say Green = "You are green!" | say Blue = "You are blue!" - | say _ = raise Fail "Unknown color" + +(* We did not include the match arm `say _ = raise Fail "Unknown color"` +because after specifying all three colors, the pattern is exhaustive +and redundancy is not permitted in pattern matching *) (* Here is a binary tree datatype *) -- cgit v1.2.3 From 6b2d12747b1297ceadf3d85192036130967fda3c Mon Sep 17 00:00:00 2001 From: Kartik Singhal Date: Tue, 8 Aug 2017 17:26:07 -0700 Subject: Use the correct identifier name --- standard-ml.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index c9eb2a2e..c286366b 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -385,7 +385,7 @@ fun calculate_interest(n) = if n < 0.0 (* Exceptions can be caught using "handle" *) val balance = calculate_interest ~180.0 - handle Domain => ~180.0 (* x now has the value ~180.0 *) + handle Domain => ~180.0 (* balance now has the value ~180.0 *) (* Some exceptions carry extra information with them *) (* Here are some examples of built-in exceptions *) -- cgit v1.2.3 From 985d23a52b76593a120adff5381c2df3a80fe298 Mon Sep 17 00:00:00 2001 From: HairyFotr Date: Wed, 23 Aug 2017 10:14:39 +0200 Subject: Fix a bunch of typos --- standard-ml.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index c286366b..9ebf345b 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -395,7 +395,7 @@ fun failing_function [] = raise Empty (* used for empty lists *) | failing_function xs = raise Fail "This list is too long!" (* We can pattern match in 'handle' to make sure - a specfic exception was raised, or grab the message *) + a specific exception was raised, or grab the message *) val err_msg = failing_function [1,2] handle Fail _ => "Fail was raised" | Domain => "Domain was raised" | Empty => "Empty was raised" -- cgit v1.2.3 From b4634d490df3d9e6b89f674d67450941c58d93ca Mon Sep 17 00:00:00 2001 From: Pratik Karki Date: Fri, 25 Aug 2017 14:11:50 +0545 Subject: Add filename(#2832) --- 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 9ebf345b..fe0f6971 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -1,5 +1,6 @@ --- language: "Standard ML" +filename: standardml.sml contributors: - ["Simon Shine", "http://shine.eu.org/"] - ["David Pedersen", "http://lonelyproton.com/"] -- cgit v1.2.3 From 5e6262e90d941059f7c5c5abda12326615ba1c4b Mon Sep 17 00:00:00 2001 From: Patrick T Coakley Date: Tue, 12 Sep 2017 11:21:57 -0400 Subject: Add a link to ML For The Working Programmer, as it has recently been released for free for personal use by the original author. --- standard-ml.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index e1fe0d19..5db15b5c 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -463,5 +463,5 @@ fun decrement_ret x y = (x := !x - 1; y) [Moscow ML](http://mosml.org), [SML/NJ](http://smlnj.org/). * Follow the Coursera course [Programming Languages](https://www.coursera.org/course/proglang). -* Get the book *ML for the Working Programmer* by Larry C. Paulson. +* Read *[ML for the Working Programmer](https://www.cl.cam.ac.uk/~lp15/MLbook/pub-details.html)* by Larry C. Paulson. * Use [StackOverflow's sml tag](http://stackoverflow.com/questions/tagged/sml). -- cgit v1.2.3 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 From fc26d05306c944efbb5280433b2e42e3dd3c9cb0 Mon Sep 17 00:00:00 2001 From: Simon Shine Date: Thu, 12 Dec 2019 07:14:11 +0100 Subject: Fix broken author homepage links, add Exercism.io link The homepages for @sshine and @davidpdrsn were broken. Add "Further reading" link to Exercism.io's SML track. --- standard-ml.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index 0ba42f39..9d6b104d 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -2,8 +2,8 @@ language: "Standard ML" filename: standardml.sml contributors: - - ["Simon Shine", "http://shine.eu.org/"] - - ["David Pedersen", "http://lonelyproton.com/"] + - ["Simon Shine", "https://simonshine.dk/"] + - ["David Pedersen", "https://github.com/davidpdrsn"] - ["James Baker", "http://www.jbaker.io/"] - ["Leo Zovic", "http://langnostic.inaimathi.ca/"] - ["Chris Wilson", "http://sencjw.com/"] @@ -479,3 +479,4 @@ fun decrement_ret x y = (x := !x - 1; y) * Follow the Coursera course [Programming Languages](https://www.coursera.org/course/proglang). * Read *[ML for the Working Programmer](https://www.cl.cam.ac.uk/~lp15/MLbook/pub-details.html)* by Larry C. Paulson. * Use [StackOverflow's sml tag](http://stackoverflow.com/questions/tagged/sml). +* Solve exercises on [Exercism.io's Standard ML track](https://exercism.io/tracks/sml). -- cgit v1.2.3