[Haskell-beginners] confusing type signature with sections

Patrick Redmond plredmond
Wed Oct 2 00:25:18 UTC 2013


I wrote code to shift and scale the range [-1, 1] to [0, 1] and mapped
it on a list.

    Prelude> map (\n -> ((n + 1) / 2)) [-1, 0, 1]
    [0.0,0.5,1.0]

I thought using sections would better express the simplicity of this operation.

    Prelude> :t (+ 1)
    (+ 1) :: Num a => a -> a

    Prelude> :t (/ 2)
    (/ 2) :: Fractional a => a -> a

Given the types of those sections, I thought it sensible for me to
compose them like this.

    Prelude> :t ((+ 1) / 2)
    ((+ 1) / 2) :: (Fractional (a -> a), Num a) => a -> a

This didn't work of course, and its type baffles me. Though it ends
with "Num a) => a -> a", it gives an error when given a number. I
don't understand what "(Fractional (a -> a), ...) =>" really means. It
seems like I've asked Haskell to perform "/" on the arguments "(+ 1)"
and "2", but that ought to be disallowed by the type of "/".

I eventually realized I should use the composition operator (below),
but I'm still curious what I created with "((+ 1) / 2)".

    Prelude> :t (/ 2) . (+ 1)
    (/ 2) . (+ 1) :: Fractional c => c -> c

Thanks!
Patrick



More information about the Beginners mailing list