user6023611
user6023611

Reputation:

haskell, case of like fmap

I have a function of type foo :: a -> a -> Either String TypeConstructor
foo can return both throwError String and something of TypeConstructor.

I would like to do something like fmap. I mean that I would like to case (foo x y z) of ... where ... means different values (it depends on used constructor value in foo).

Is there exista any way to do it ?

Upvotes: 1

Views: 135

Answers (2)

Chris Taylor
Chris Taylor

Reputation: 47392

You can't directly write

case (foo x y) of ...

and then pattern match on the type constructors of TypeConstructor since foo x y does not have the correct type (it is Either String TypeConstructor).

However, you can define a function which pattern matches on the type constructors of TypeConstructor and then fmap this over the result of foo x y as shown below.

import Control.Monad.Except (throwError)

data Type = Int Int
          | Str String
          deriving (Show)

foo :: Int -> String -> Either String Type
foo n s =
    if n == 0
        then throwError "Zero"
        else return (Str s)

bar x y = fmap f (foo x y)
    where
        f a = case a of
            Int n -> Int (n +  x)
            Str s -> Str (s ++ y)

Upvotes: 2

chi
chi

Reputation: 116139

A basic way could be to pattern match everything

case foo x y of
  Left string -> ...
  Right (Cons1 z w) -> ...
  Right (Cons2 a b c) -> ...
  Right Cons3 -> ...

where Cons1, ... are the value constructors of TypeConstructor.

Upvotes: 2

Related Questions