Home > Back-end >  Haskell function implementing
Haskell function implementing

Time:11-11

I have next data type:

newtype Fun i a = F (i -> a)

And I have to implement function with next signature:

joinFun :: Fun i (Fun i a) -> Fun i a

I tried

joinFun (F f) = f

but it have type i -> Fun i a
How can I implement this function with right types?

CodePudding user response:

f has as type i -> Fun i a, and you are supposed to return a Fun i a, so a function that maps an i to an a.

The only sensical thing to do is construct a function that takes a parameter of type i, that will be used on f, this will then return a Fun i a, where we will evaluate that function by applying it again with that parameter i.

We thus can implement joinFun with:

joinFun :: Fun i (Fun i a) -> Fun i a
joinFun (F f) = F go
    where go i = case f i of
              (F g) -> …

where I leave implementing the part as an exercise.

  • Related