Fraction reduction: Difference between revisions

m
→‎{{header|Haskell}}: this foldr is just a map and filter which can be written as a list comprehension
m (→‎{{header|Haskell}}: this foldr is just a map and filter which can be written as a list comprehension)
Line 1,379:
 
findReductions :: Fraction -> [Reduction]
findReductions z@(n1, d1) = foldr[ f(z, [](n2, d2), sharedDigitsx)
sharedDigits = | x <- digits 10 n1 `intersect` digits 10 d1,
where
let n2 = dropDigit x n1
sharedDigits = digits 10 n1 `intersect` digits 10 d1
d2 = dropDigit x d1
decimal = realToFrac n1 / realToFrac d1
decimalWithDrop = realToFrac n2 / realToFrac d2,
f x r | decimalWithDrop == decimal = (z, (n2, d2), x) : r
| otherwise decimalWithDrop == decimal r]
where dropDigit d = digitsToIntegral . delete d . digits 10
where
dropDigit ddecimal = digitsToIntegralrealToFrac .n1 delete/ drealToFrac . digits 10d1
n2 = dropDigit x n1
d2 = dropDigit x d1
decimalWithDrop = realToFrac n2 / realToFrac d2
 
findGroupReductions :: [Int] -> [Reduction]
Anonymous user