SOE ex.9.4


applyEach :: [ a -> b ] -> a -> [b]
applyEach fs x = zipWith id fs (repeat x)

applyEach' :: [ a -> b ] -> a -> [b]
applyEach' fs = (zipWith id fs) . repeat  -- eta reduction

applyEach'' :: [ a -> b ] -> a -> [b]
applyEach'' fs x = [ f x | f <- fs ]

applyEach''' :: [ a -> b ] -> a -> [b]
applyEach''' [] _ = []
applyEach''' (f:fs) x = f x : applyEach''' fs x


----- test -----
main = do
  putStrLn $ show $ applyEach    [simple 2 2, (+3)] 5
  putStrLn $ show $ applyEach'   [simple 2 2, (+3)] 5
  putStrLn $ show $ applyEach''  [simple 2 2, (+3)] 5
  putStrLn $ show $ applyEach''' [simple 2 2, (+3)] 5

{-
    applyEach :: [ a -> b ] -> a -> [b]
    applyEach fs x = zipWith g fs (repeat x)

    g :: (a -> b) -> a -> b
    g f x = f x
<=> g f = f  -- after eta reduction
<=> g = ?    -- after eta reduction

    g = id   -- id :: a -> a
    g (f x) = (f x)
-}

simple :: Int -> Int -> Int -> Int
simple x y z = x * (y+z)


Leave a Reply