[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'
This commit is contained in:
Wim 2016-08-04 14:41:16 +02:00 committed by ven
parent 25aa41467a
commit 2f28300d10

View File

@ -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 ]
| [] -> []