[Haskell-cafe] arr f

Henrik Nilsson nhn at Cs.Nott.AC.UK
Fri Jan 27 15:47:53 EST 2006


Hi Sven,

 > since this is my first post,
 >
 >    "Hello all"

Welcome!

 > I have a problem with a function taken from class Arrow.
 >
 >    arr :: (b -> c) -> a b c
 >
 > To build my own arrow, i need to distinguish between different kinds
 > of b or c. For instance, if b has the form (d,e), i want to construct
 > something different as when having form (d,e,f).

I'm not quite sure I understand what you mean.

Are you saying that you you want the input to be either a pair or a
triple?

Then one possibility would be to wrap up the input in the standard
Haskell type "Either".

For reference, "Either" is defined like this:

     data Either a b = Left a | Right b

Assume

    g :: T1 -> T2 -> T
    h :: T1 -> T2 -> T3 -> T

for some specific types T1, T2, T3 and T.

Now you can define a function "f":

    f :: Either (T1,t2) (T1,T2,T3) -> T
    f (Left  (x,y))   = g x y
    f (Right (x,y,z)) = h x y z

Thus "f" "distinguish[es] between different kinds" of input.
If it is applied to what essentially is a pair, it will compute
a result of type "T" using the function "g", whereas if it is
applied to what essentially is a triple it will compute
a result using the function "h", again of type "T".

Lifting "f" into an arrow yields:

    arr f :: Arrow a => a (Either (T1,T2) (T1,T2,T3)) T

I don't know if that was what you really ment by "construct[ing]
something different" depending on the input type?

Hope that helps,

/Henrik

-- 
Henrik Nilsson
School of Computer Science and Information Technology
The University of Nottingham
nhn at cs.nott.ac.uk


This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.



More information about the Haskell-Cafe mailing list