annotate 7-Haskell/factorial.hs @ 101:1fae0cca1ef8

Reduce large maze to single width corridors This reduces the permutations for a x x x b x To one (two steps north) from four (two steps north; one east, two north, one west; one east, one north, one west, one north; and one north, one east, one north, one west). Longer corridors were worse! We would filter this in the "been here before via another path" but that's still a lot of lookups in lists, which is inefficient.
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Jul 2019 13:42:24 +0100
parents 2b5341fc4555
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
87
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 module Factorial where
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 -- The book says to use "Main", but that now triggers "The IO action ‘main’ is not defined in module ‘Main’". https://optimistictypes.com/compiler-errors/#module-main-where says Main is special and must have a function "main".
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 -- Instead we'll use our own module name, because it doesn't seem to be important (yet).
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 -- Without pattern matching
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 fact :: Integer -> Integer
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 -- Note how this is like a Python "x if a else y"
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 fact x = if x == 0 then 1 else x * fact (x - 1)
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 -- With pattern matching
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 factorial :: Integer -> Integer
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 factorial 0 = 1
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 factorial x = x * factorial(x - 1)
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 -- With guards
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 factorial_guard :: Integer -> Integer
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 factorial_guard x
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 -- List of definitions, which look similar to the pattern matching but without the function name
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 | x > 1 = x * factorial(x - 1)
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 -- And the fallback "otherwise" result
2b5341fc4555 Add Haskell Day 1 code and notes
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 | otherwise = 1