Project Inquiry

Implement Prelude.flip

Challenge
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude hiding (flip)

main :: IO ()
main = putStrLn $ if test then "SUCCESS" else "FAILURE"

test :: Bool
test = and [ (flip helper) True [1..5] == [2, 4]
           , (flip helper) False [1..5] == [1, 3, 5]
           ]
  where helper :: (Integral a) => [a] -> Bool -> [a]
        helper xs True  = filter even xs
        helper xs False = filter odd xs

-- TODO: This program doesn't compile. Define the missing function(s) incl. type signatures!
Solution
{-# LANGUAGE NoImplicitPrelude #-}

module Main where

import           Prelude hiding (flip)

main :: IO ()
main = putStrLn $ if test then "SUCCESS" else "FAILURE"

test :: Bool
test = and [ (flip helper) True [1..5] == [2, 4]
           , (flip helper) False [1..5] == [1, 3, 5]
           ]
  where helper :: (Integral a) => [a] -> Bool -> [a]
        helper xs True  = filter even xs
        helper xs False = filter odd xs

-- DONE
flip :: (a -> b -> c) -> b -> a -> c
flip f fstParam sndParam = f sndParam fstParam