Skip to content

Commit 79c006c

Browse files
committed
Haskell: Use ormolu to format all code
1 parent 5b8a2a8 commit 79c006c

File tree

18 files changed

+182
-38
lines changed

18 files changed

+182
-38
lines changed

haskell/haskell.mk

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: default run clean
1+
.PHONY: default run clean format
22

33
default: main
44

@@ -12,3 +12,6 @@ clean:
1212
find . -type f -name '*.hi' -delete
1313
find . -type f -name '*.o' -delete
1414
rm -f main
15+
16+
format:
17+
ormolu --mode inplace $(shell find . -name '*.hs')

haskell/problem-001/improved/main.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ main = print answer
33

44
answer :: Integer
55
answer = sum (multiplesBelow 1000)
6-
where multiples = [n | n <- [1..], n `mod` 3 == 0 || n `mod` 5 == 0]
7-
multiplesBelow x = takeWhile (< x) multiples
6+
where
7+
multiples = [n | n <- [1 ..], n `mod` 3 == 0 || n `mod` 5 == 0]
8+
multiplesBelow x = takeWhile (< x) multiples

haskell/problem-002/improved/main.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ answer :: Integer
55
answer = sum . filter even . takeWhile (< 4000000) $ fibList
66

77
fibList :: [Integer]
8-
fibList = map fib [0..]
9-
where fib 0 = 0
10-
fib 1 = 1
11-
fib n = (fibList !! (n - 2)) + (fibList !! (n - 1))
8+
fibList = map fib [0 ..]
9+
where
10+
fib 0 = 0
11+
fib 1 = 1
12+
fib n = (fibList !! (n - 2)) + (fibList !! (n - 1))
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
module Utilities.List (none)
2-
where
1+
module Utilities.List (none) where
32

43
none :: (a -> Bool) -> [a] -> Bool
54
none f l = not (any f l)
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
module Utilities.Math (isFactorOf)
2-
where
1+
module Utilities.Math (isFactorOf) where
32

43
isFactorOf :: Integer -> Integer -> Bool
54
isFactorOf divisor dividend = dividend `mod` divisor == 0

haskell/problem-003/improved/main.hs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ answer = gpf 600851475143
99

1010
gpf :: Integer -> Integer
1111
gpf n = last primeFactors
12-
where primeFactors = filter (isPrime) (factors n)
12+
where
13+
primeFactors = filter (isPrime) (factors n)
1314

1415
isPrime :: Integer -> Bool
15-
isPrime n = none (\x -> x `isFactorOf` n) [2..(flooredSquareRoot n)]
16+
isPrime n = none (\x -> x `isFactorOf` n) [2 .. (flooredSquareRoot n)]
1617

1718
factors :: Integer -> [Integer]
18-
factors n = foldr concatFactors [] [1..(flooredSquareRoot n)]
19-
where concatFactors x acc =
20-
if (x `isFactorOf` n)
21-
then if (n `div` x == x)
22-
then x : acc
23-
else x : acc ++ [n `div` x]
24-
else acc
19+
factors n = foldr concatFactors [] [1 .. (flooredSquareRoot n)]
20+
where
21+
concatFactors x acc =
22+
if (x `isFactorOf` n)
23+
then
24+
if (n `div` x == x)
25+
then x : acc
26+
else x : acc ++ [n `div` x]
27+
else acc
2528

2629
flooredSquareRoot :: Integer -> Integer
2730
flooredSquareRoot = floor . sqrt . fromIntegral

haskell/problem-004/improved/main.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ main = print answer
33

44
answer :: Integer
55
answer = maximum . filter isPalindrome $ products
6-
where products = [x * y | x <- threeDigitNumbers, y <- threeDigitNumbers]
7-
threeDigitNumbers = [999,998..100]
6+
where
7+
products = [x * y | x <- threeDigitNumbers, y <- threeDigitNumbers]
8+
threeDigitNumbers = [999, 998 .. 100]
89

910
isPalindrome :: Integer -> Bool
1011
isPalindrome n = (str == reverse str)
11-
where str = show n
12+
where
13+
str = show n

haskell/problem-005/improved/main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ main :: IO ()
22
main = print answer
33

44
answer :: Integer
5-
answer = lcmMultiple [1..20]
5+
answer = lcmMultiple [1 .. 20]
66

77
lcmMultiple :: [Integer] -> Integer
88
lcmMultiple l = foldl lcm 1 l

haskell/problem-006/improved/main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ main :: IO ()
22
main = print answer
33

44
answer :: Integer
5-
answer = (squareOfSum [1..100]) - (sumOfSquares [1..100])
5+
answer = (squareOfSum [1 .. 100]) - (sumOfSquares [1 .. 100])
66

77
squareOfSum :: [Integer] -> Integer
88
squareOfSum = square . sum
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
module Primes (primes, sieveOfEratosthenes)
2-
where
1+
module Primes (primes, sieveOfEratosthenes) where
32

43
import Utilities.Math
54

65
type Sieve = [Integer] -> [Integer]
76

87
primes :: Sieve -> [Integer]
98
primes sieve = sieve candidates
10-
where candidates = 2 : [3,5..]
9+
where
10+
candidates = 2 : [3, 5 ..]
1111

1212
sieveOfEratosthenes :: Sieve
13-
sieveOfEratosthenes (prime:otherCandidates) = prime : (sieveOfEratosthenes nonFactors)
14-
where nonFactors = filter (\x -> not (prime `isFactorOf` x)) otherCandidates
13+
sieveOfEratosthenes (prime : otherCandidates) = prime : (sieveOfEratosthenes nonFactors)
14+
where
15+
nonFactors = filter (\x -> not (prime `isFactorOf` x)) otherCandidates
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
module Utilities.Math (isFactorOf)
2-
where
1+
module Utilities.Math (isFactorOf) where
32

43
isFactorOf :: Integer -> Integer -> Bool
54
isFactorOf divisor dividend = dividend `mod` divisor == 0

haskell/problem-008/improved/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ../../haskell.mk

haskell/problem-008/improved/main.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Data.Char (digitToInt)
2+
3+
main :: IO ()
4+
main = print answer
5+
6+
answer :: Integer
7+
answer = greatestAdjacentProduct number adjacent
8+
9+
number :: Integer
10+
number = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
11+
12+
adjacent :: Integer
13+
adjacent = 13
14+
15+
greatestAdjacentProduct :: Integer -> Integer -> Integer
16+
greatestAdjacentProduct number adjacent = findGAP numberString adjacentInt initialBest
17+
where
18+
numberString = show number
19+
adjacentInt = fromIntegral adjacent
20+
initialBest = 0
21+
findGAP n a best
22+
| (length n) < a = best
23+
| otherwise = findGAP remaining a newBest
24+
where
25+
remaining = drop 1 n
26+
newBest = max best (fromIntegral currentProduct)
27+
currentProduct = product . map digitToInt . take a $ n

haskell/problem-009/improved/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ../../haskell.mk

haskell/problem-009/improved/main.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Data.List
2+
3+
main :: IO ()
4+
main = print answer
5+
6+
answer :: Integer
7+
answer = tripletProduct . head . pythagoreanTriplesWithSum $ targetSum
8+
where
9+
tripletProduct (a, b, c) = a * b * c
10+
targetSum = 1000
11+
12+
-- When determining possible values for A and B, we must remember that B and C
13+
-- cannot be zero. Thus the first possible A value is sum-2 (so that B and C can
14+
-- be 1) and the first possible B value is sum-a-1 (so that C can be 1).
15+
pythagoreanTriplesWithSum :: Integer -> [(Integer, Integer, Integer)]
16+
pythagoreanTriplesWithSum sum = nubBy (\(a1, b1, c1) (a2, b2, c2) -> a1 + b1 + c1 == a2 + b2 + c2) allTriples
17+
where
18+
allTriples = [(a, b, c) | a <- [(sum -2), (sum -3) .. 1], b <- [(sum - a -1), (sum - a -2) .. 1], let c = sum - a - b, a ^ 2 + b ^ 2 == c ^ 2]

haskell/problem-010/improved/main.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ answer = sumOfPrimesBelow 2000000
88

99
sumOfPrimesBelow :: Int -> Int
1010
sumOfPrimesBelow n = sum naturals - sum composites - sum neitherPrimeNorComposite
11-
where naturals = [1..n]
12-
composites = compositesUntil n
13-
neitherPrimeNorComposite = [1]
11+
where
12+
naturals = [1 .. n]
13+
composites = compositesUntil n
14+
neitherPrimeNorComposite = [1]
1415

1516
-- Return a set of composites up to and including the number "until". This
1617
-- algorithm works something like the "marking" stage of the Sieve of
@@ -30,6 +31,7 @@ sumOfPrimesBelow n = sum naturals - sum composites - sum neitherPrimeNorComposit
3031
-- the factor and we ignore all multiples greater than "until".
3132
compositesUntil :: Int -> Set Int
3233
compositesUntil until = Set.foldl unionMultiples Set.empty factors
33-
where unionMultiples acc n = Set.union acc (multiples n)
34-
factors = Set.fromDistinctAscList ([2] ++ [3,5..(floor $ sqrt $ fromIntegral until)])
35-
multiples n = Set.fromDistinctAscList [n ^ 2, n ^ 2 + n..until]
34+
where
35+
unionMultiples acc n = Set.union acc (multiples n)
36+
factors = Set.fromDistinctAscList ([2] ++ [3, 5 .. (floor $ sqrt $ fromIntegral until)])
37+
multiples n = Set.fromDistinctAscList [n ^ 2, n ^ 2 + n .. until]

haskell/problem-011/improved/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ../../haskell.mk

haskell/problem-011/improved/main.hs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
main :: IO ()
2+
main = print answer
3+
4+
answer :: Int
5+
answer = maximum products
6+
7+
products :: [Int]
8+
products = concat $ map productsAtPoint points
9+
where
10+
points = [(x, y) | x <- [0 .. (size - 1)], y <- [0 .. (size - 1)]]
11+
12+
productsAtPoint :: (Int, Int) -> [Int]
13+
productsAtPoint point =
14+
[ horizontalProduct point,
15+
verticalProduct point,
16+
forwardDiagonalProduct point,
17+
backwardDiagonalProduct point
18+
]
19+
20+
horizontalProduct :: (Int, Int) -> Int
21+
horizontalProduct (x, y)
22+
| x > (size - 4) = 0
23+
| otherwise =
24+
point x y
25+
* point (x + 1) y
26+
* point (x + 2) y
27+
* point (x + 3) y
28+
29+
verticalProduct :: (Int, Int) -> Int
30+
verticalProduct (x, y)
31+
| y > (size - 4) = 0
32+
| otherwise =
33+
point x y
34+
* point x (y + 1)
35+
* point x (y + 2)
36+
* point x (y + 3)
37+
38+
forwardDiagonalProduct :: (Int, Int) -> Int
39+
forwardDiagonalProduct (x, y)
40+
| x > (size - 4) = 0
41+
| y > (size - 4) = 0
42+
| otherwise =
43+
point x y
44+
* point (x + 1) (y + 1)
45+
* point (x + 2) (y + 2)
46+
* point (x + 3) (y + 3)
47+
48+
backwardDiagonalProduct :: (Int, Int) -> Int
49+
backwardDiagonalProduct (x, y)
50+
| x < 3 = 0
51+
| y > (size - 4) = 0
52+
| otherwise =
53+
point x y
54+
* point (x - 1) (y + 1)
55+
* point (x - 2) (y + 2)
56+
* point (x - 3) (y + 3)
57+
58+
point :: Int -> Int -> Int
59+
point x y = (grid !! y) !! x
60+
61+
size :: Int
62+
size = length grid
63+
64+
grid :: [[Int]]
65+
grid =
66+
[ [08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08],
67+
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00],
68+
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65],
69+
[52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91],
70+
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
71+
[24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
72+
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
73+
[67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 08, 40, 91, 66, 49, 94, 21],
74+
[24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
75+
[21, 36, 23, 09, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95],
76+
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 09, 53, 56, 92],
77+
[16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57],
78+
[86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
79+
[19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40],
80+
[04, 52, 08, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
81+
[88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
82+
[04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 08, 46, 29, 32, 40, 62, 76, 36],
83+
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16],
84+
[20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54],
85+
[01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48]
86+
]

0 commit comments

Comments
 (0)