История изменений
Исправление hateyoufeel, (текущая версия) :
Держи на Haskell
import System.IO.Unsafe
p = unsafePerformIO . putStrLn
f acc x = p ("acc(" <> show acc <> ") + " <> show x)
class SumRes r where
sumOf :: Integer -> r
instance SumRes () where
sumOf _ = ()
instance (Show a, Integral a, SumRes r) => SumRes (a -> r) where
sumOf x acc = f acc x `seq` sumOf (x + toInteger acc)
main = (sumOf 10 20 30 40 :: ()) `seq` return ()
Вывод:
$ ./carrot
acc(10) + 20
acc(30) + 30
acc(60) + 40
Исходная версия hateyoufeel, :
Держи на Haskell
import System.IO.Unsafe
p = unsafePerformIO . putStrLn
f acc x = p ("acc(" <> show acc <> ") + " <> show x)
class SumRes r where
sumOf :: Integer -> r
instance SumRes () where
sumOf _ = ()
instance (Show a, Integral a, SumRes r) => SumRes (a -> r) where
sumOf x acc = f acc x `seq` sumOf ((x +) . toInteger $ acc)
main = (sumOf 10 20 30 40 :: ()) `seq` return ()
Вывод:
$ ./carrot
acc(10) + 20
acc(30) + 30
acc(60) + 40