From e76f7cd4525e135599719af7379b29ce625ccb6f Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 5 Dec 2013 11:45:43 +0100 Subject: [standard-ml/en-en] Update to list part --- standard-ml.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index ab5e4b84..f7526bb8 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -95,7 +95,8 @@ val groups = [ [ "Alice", "Bob" ], val number_count = List.length numbers (* gives 7 *) -(* You can put single values in front of lists of the same kind *) +(* You can put single values in front of lists of the same kind + using the :: ("cons") operator *) val more_numbers = 13 :: numbers (* gives [13, 1, 3, 3, 7, ...] *) val more_groups = ["Batman","Superman"] :: groups -- cgit v1.2.3 From ed82f1d8682ce6e5360177c216dc3c4eb7a148d0 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 5 Dec 2013 11:47:48 +0100 Subject: [standard-ml/en-en] Add missing comma --- 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 f7526bb8..730ef445 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -328,7 +328,7 @@ val test_poem = readPoem "roses.txt" (* gives [ "Roses are red,", * Install an interactive compiler (REPL), for example [Poly/ML](http://www.polyml.org/), - [Moscow ML](http://mosml.org) + [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. -- cgit v1.2.3 From b8857e92663aab608b0afe6e2e79949908ac205b Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 5 Dec 2013 11:48:02 +0100 Subject: [standard-ml/en-en] Add myself as a collaborator --- 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 730ef445..b5fef910 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -2,6 +2,7 @@ language: Standard ML contributors: - ["Simon Shine", "http://shine.eu.org/"] + - ["David Pedersen", "http://lonelyproton.com/"] lang: en-en --- -- cgit v1.2.3 From 972a4bf80270a64466beb6fb882093cf7100dcc5 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 5 Dec 2013 11:50:45 +0100 Subject: [standard-ml/en-en] Update list doc again --- 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 b5fef910..e17972a0 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -104,7 +104,7 @@ val more_groups = ["Batman","Superman"] :: groups (* Lists of the same kind can be appended using the @ ("append") operator *) val guest_list = [ "Mom", "Dad" ] @ [ "Aunt", "Uncle" ] -(* This could have been done with the :: operator (pronounced "cons") *) +(* This could have been done with the "cons" operator *) val guest_list = "Mom" :: "Dad" :: [ "Aunt", "Uncle" ] (* If you have many lists of the same kind, you can concatenate them all *) -- cgit v1.2.3 From f1c96d5db0dae92e13be96a03118da2565ab3327 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 5 Dec 2013 11:51:56 +0100 Subject: [standard-ml/en-en] Small whitespace fix --- 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 e17972a0..56b6853c 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -186,7 +186,7 @@ fun fibonacci n = temporarily shadow them with new variables that have the same names. In this sense, variables are really constants and only behave like variables when dealing with recursion. For this reason, variables are also called value - bindings. An example of this: *) + bindings. An example of this: *) val x = 42 fun answer(question) = -- cgit v1.2.3 From a03869362b5706345ecbb3783bd7f6173d12b698 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 5 Dec 2013 12:33:02 +0100 Subject: [standard-ml/en-en] Exceptions --- standard-ml.html.markdown | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index 56b6853c..547ca39b 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -302,6 +302,28 @@ fun count (Leaf n) = n | count (Node (leftTree, n, rightTree)) = count leftTree + n + count rightTree +(* Exceptions! *) +(* Exceptions can be raised using "raise" *) +fun raiseException msg = raise Fail msg + +(* This raises exception `Fail "hello from exception"` *) +(* val _ = raiseException "hello from exception" *) + +(* Exceptions can be caught using "handle" *) +val x = raiseException "hello" handle Fail msg => msg +(* x now has the value "hello" *) + +(* We can pattern match in "handle" to make sure + a specfic exception was raised, or grab the message *) +val y = raiseException "..." handle Fail _ => "Fail was raised" + | Domain => "Domain was raised" +(* y now has the value "Fail was raised" *) + +(* We can define our own exceptions like this *) +exception MyException +exception MyExceptionWithMessage of string + + (* File I/O! *) (* Write a nice poem to a file *) fun writePoem(filename) = -- cgit v1.2.3 From acc2dda568ac3bad9173a1cf3f114905a102d893 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 5 Dec 2013 12:34:04 +0100 Subject: [standard-ml/en-en] align things a little --- 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 547ca39b..9de31340 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -316,7 +316,7 @@ val x = raiseException "hello" handle Fail msg => msg (* We can pattern match in "handle" to make sure a specfic exception was raised, or grab the message *) val y = raiseException "..." handle Fail _ => "Fail was raised" - | Domain => "Domain was raised" + | Domain => "Domain was raised" (* y now has the value "Fail was raised" *) (* We can define our own exceptions like this *) -- cgit v1.2.3 From d71ac35d72529d3dee08d9591cd1c65cbdcccb6d Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 5 Dec 2013 12:43:13 +0100 Subject: [standard-ml/en-en] infix functions --- standard-ml.html.markdown | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'standard-ml.html.markdown') diff --git a/standard-ml.html.markdown b/standard-ml.html.markdown index 9de31340..849ba0f5 100644 --- a/standard-ml.html.markdown +++ b/standard-ml.html.markdown @@ -264,6 +264,24 @@ fun map f [] = [] (* map has type ('a -> 'b) -> 'a list -> 'b list and is called polymorphic. *) (* 'a is called a type variable. *) + +(* We can define functions as infix *) +fun plus (x, y) = x + y +infix plus +(* We can now call plus like "2 plus 5" *) + +(* Functions can also be made infix before they are defined *) +infix minus +fun x minus y = x - y + +(* An infix function/operator can be made prefix with "op" *) +val n = op + (5, 5) +(* n is now 10 *) + +(* op is useful when combined with high order functions *) +val listSum = foldl op + 0 [1,2,3,4,5] + + (* Datatypes are useful for creating both simple and complex structures *) datatype color = Red | Green | Blue -- cgit v1.2.3