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 <ren...@mathematik.uni-marburg.de> wrote:

From: Tillmann Rendel <ren...@mathematik.uni-marburg.de>
Subject: Re: [Haskell-cafe] On to applicative
To: "michael rice" <nowg...@yahoo.com>
Cc: haskell-cafe@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?



      
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to