From 2f28300d10505c294ae1d805514c7d01636925f2 Mon Sep 17 00:00:00 2001 From: Wim Date: Thu, 4 Aug 2016 14:41:16 +0200 Subject: [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' --- fsharp.html.markdown | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'fsharp.html.markdown') 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 ] | [] -> [] -- cgit v1.2.3