summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorWim <wimhoringa@gmail.com>2016-08-04 14:41:16 +0200
committerven <vendethiel@hotmail.fr>2016-08-04 14:41:16 +0200
commit2f28300d10505c294ae1d805514c7d01636925f2 (patch)
tree7f30f57cd3e3ac4143c237d19ac9b420398a96e6
parent25aa41467a13fbc98de3a0a058bd5ecc2f03b5c3 (diff)
[fsharp/en] Explain the cons pattern, and introduce recursion keyword (#2310)
* [fsharp/en] Explain the cons pattern, and introduce recursion keyword Was confused when reading the sieve function and thought it could be explained a little more. I got some help from http://hestia.typepad.com/flatlander/2010/07/f-pattern-matching-for-beginners-part-4-lists-and-recursion.html * Forgot the word 'notation'
-rw-r--r--fsharp.html.markdown7
1 files changed, 6 insertions, 1 deletions
diff --git a/fsharp.html.markdown b/fsharp.html.markdown
index e345201d..69f4eb60 100644
--- a/fsharp.html.markdown
+++ b/fsharp.html.markdown
@@ -175,7 +175,12 @@ module ListExamples =
// list comprehensions (aka generators)
let squares = [for i in 1..10 do yield i * i]
- // prime number generator
+ // A prime number generator
+ // - this is using a short notation for the pattern matching syntax
+ // - (p::xs) is 'first :: tail' of the list, could also be written as p :: xs
+ // this means this matches 'p' (the first item in the list), and xs is the rest of the list
+ // this is called the 'cons pattern'
+ // - uses 'rec' keyword, which is necessary when using recursion
let rec sieve = function
| (p::xs) -> p :: sieve [ for x in xs do if x % p > 0 then yield x ]
| [] -> []