-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.sml
39 lines (29 loc) · 950 Bytes
/
4.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(*
* A palindromic number reads the same both ways. The largest palindrome made
* from the product of two 2-digit numbers is 9009 = 91 99.
*
* Find the largest palindrome made from the product of two 3-digit numbers.
*)
use "lazy_utils.sml";
fun isPalindrome n =
let
val digits = explode(Int.toString(n));
in
digits = rev(digits)
end;
fun rangeUp a b =
(takeWhile (fn x => x < b)) (iterate (fn x => x + 1) a);
fun rangeDown a b =
(takeWhile (fn x => x > b)) (iterate (fn x => x - 1) a);
fun comprehend f xs ys =
map (fn x => map (f x) ys) xs;
fun allProducts xs ys =
comprehend (fn x => fn y => x * y) xs ys;
allProducts (rangeDown 10 1) (rangeDown 10 1);
fun largestProducts xs ys =
map (fn nil => (valOf Int.minInt)
| x::xs => x)
(map (List.filter isPalindrome) (allProducts xs ys));
val answer =
(foldl Int.max (valOf Int.minInt))
(largestProducts (rangeDown 999 100) (rangeDown 999 100));