[Haskell-cafe] On to applicative
michael rice
nowgate at yahoo.com
Wed Sep 1 18:29:49 EDT 2010
Hi Tillman,
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> :t z
z :: Integer -> Integer
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
that would require x to have a declared type, or is it always inferred by the type of f?
Michael
--- On Wed, 9/1/10, Tillmann Rendel <rendel at Mathematik.Uni-Marburg.de> wrote:
From: Tillmann Rendel <rendel at Mathematik.Uni-Marburg.de>
Subject: Re: [Haskell-cafe] On to applicative
To: "michael rice" <nowgate at yahoo.com>
Cc: haskell-cafe at haskell.org
Date: Wednesday, September 1, 2010, 5:28 PM
michael rice wrote:
> 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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100901/e2df7264/attachment.html
More information about the Haskell-Cafe
mailing list