Home > Mobile >  Writing a function in Haskell
Writing a function in Haskell

Time:04-08

I want to write a function for the co :: (b -> c) -> (a -> b) -> a -> c. Can someone please provide a clear and simple explanation on how to write a Haskell function for co :: (b -> c) -> (a -> b) -> a -> c

I am working thru the chapter 5 exercises (given the type, write the function) in the Haskell programming from first principles, the question reads:

Only one version will type check:

co :: (b -> c) -> (a -> b) -> a -> c 
co = undefined

I am not sure how to start this problem

CodePudding user response:

I am not sure how to start this problem

This is how:

co :: (b -> c) -> (a -> b) -> a -> c 
co       = undefined
co f     = undefined  -- f ::      b -> c    co f :: (a -> b) -> a -> c 
co f g   = undefined  -- g :: a -> b         co f g ::           a -> c
co f g a = c          -- a :: a              co f g a ::              c
  where               -- c ::           c
  c = undefined

So that's the few first steps we can make. Now,
what can produce a c for us? The f can:

  c = f b
  b = undefined

And that's another step. But now we need a b.
What can produce a b for us? The g can:

  b = g a

And can we get an a somewhere? Do we need to?

See also: Defining a function that satisfies a type in Haskell

  • Related