Professional Documents
Culture Documents
Programming in Haskell: Chapter 9 - The Countdown Problem
Programming in Haskell: Chapter 9 - The Countdown Problem
Programming in Haskell: Chapter 9 - The Countdown Problem
1
What Is Countdown?
2
Example
1 3 7 10 25 50
+ -
3
Rules
4
For our example, one possible solution is
Notes:
Operators:
Apply an operator:
Expressions:
7
Return the overall value of an expression, provided
that it is a positive natural number:
8
Formalising The Problem
For example:
[[],[1],[2],[1,2],[2,1]]
9
Return a list of all the values in an expression:
10
Brute Force Solution
For example:
[([1],[2,3,4]),([1,2],[3,4]),([1,2,3],[4])]
11
Return a list of all possible expressions whose values
are precisely a given list of numbers:
14
Can We Do Better?
15
Fusing Two Functions
results [] = []
results [n] = [(Val n,n) | n > 0]
results ns =
[res | (ls,rs) split ns
, lx results ls
, ry results rs
, res combine' lx ry]
where
17
Combining results:
19
Can We Do Better?
xy = yx
x1 = x
Around 20
Valid: 250,000 expressions
times less.
Around 16
Solutions: 49 expressions
times less.
22
One solution: 0.007 seconds Around 2
times faster.
Around 11
All solutions: 0.119 seconds times faster.