
Sorry, my last message got garbled. Hope this is better.
Prelude Control.Monad Control.Monad.Instances Control.Applicative> let f = \x -> x:[]
Prelude Control.Monad Control.Monad.Instances Control.Applicative> :t f
f :: a -> [a]
Prelude Control.Monad Control.Monad.Instances Control.Applicative> let g = \x -> Just x
Prelude Control.Monad Control.Monad.Instances Control.Applicative> :t g
g :: a -> Maybe a
=====
Prelude Control.Monad Control.Monad.Instances Control.Applicative> let z = \x -> x+1
Prelude Control.Monad Control.Monad.Instances Control.Applicative> :t z
z :: Integer -> Integer
Prelude Control.Monad Control.Monad.Instances Control.Applicative.Data.Char> let y = \x -> ord x
Prelude Control.Monad Control.Monad.Instances Control.Applicative Data.Char> :t y
y :: Char -> Int
Can you think of a situation for
\x -> f x
or
\x y z -> x + ord y - head z
that would require x (y z) to have their type(s) declared (ala Pascal), or is it always
inferred by what appears to the right of "->"?
I guess what I'm asking is can an anonymous function be given a type signature?
Michael
--- On Wed, 9/1/10, Tillmann Rendel
Prelude Data.Either> let m = Just 7 Prelude Data.Either> :t m m :: Maybe Integer
So to create a value of type (Maybe ...), you can use Just.
Prelude Data.Either> let l = 2:[] Prelude Data.Either> :t l l :: [Integer]
So to create a value of type [...], you can use (:) and [].
Prelude Data.Either> let g = getLine Prelude Data.Either> :t g g :: IO String
So to create a value of type (IO ...), you can use getLine.
Prelude Data.Either> let e = Right "abc" Prelude Data.Either> :t e e :: Either a [Char]
So to create a value of type (Either ... ...), you can use Right.
How can I similarly create an instance of (->) [...] ?
An "instance of (->)" is usually called a function. And functions are created by lambda abstraction: Prelude> let f = \x -> x Prelude> :t f f :: t -> t So to create a value of type (... -> ...), you can use \. Just like Either, -> is a binary type constructor. Just like (Either a b) is a type, (a -> b) is a type. And very much like you can create (Either a b) values with Left and Right, you can create (a -> b) values with \. Tillmann PS. After studying how to construct values, it may be instructive to study how to take them apart. For example, Bool values can be taken apart by if-then-else expressions. What about Either, IO and -> values?