Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Questions about lambda calculus (Patrick LeBoutillier)
   2.  Re: Eliminate repetitive monad/list code (John Smith)
   3.  State Monads (and OpenGL) (Tom Hobbs)


----------------------------------------------------------------------

Message: 1
Date: Thu, 4 Nov 2010 14:02:48 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: [Haskell-beginners] Questions about lambda calculus
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi all,

I've been reading this article: http://perl.plover.com/lambda/tpj.html
in which the author talks about
the lambda calculus and uses examples in Perl (here's a link directly
to the code: http://perl.plover.com/lambda/lambda-brief.pl)
I'm a total newbie with respect to the lambda calculus.
fA = \x -> 'A'
fB = \x -> 'B'
x -=- y = (show $ x id) == (show $ y id)
I tried (naively) to port these examples to Haskell to play a bit with them:


fTRUE = \a -> \b -> a
fFALSE = \a -> \b -> b

fIF = \b -> \x -> \y -> b x y

fPAIR   = \a -> \b -> \f -> f a b
fFIRST  = \p -> p fTRUE
fSECOND = \p -> p fFALSE

fZERO    = fPAIR fTRUE fTRUE
fSUCC    = \x -> fPAIR fFALSE x
fIS_ZERO = \x -> fFIRST x
fPRED    = \x -> fSECOND x
fONE     = fSUCC fZERO
fTWO     = fSUCC fONE

fADD = \m -> (\n -> fIF (fIS_ZERO m) n (fADD (fPRED m) (fSUCC n)))

but I couldn't get fADD to compile:

   Occurs check: cannot construct the infinite type:
      t = (t1 -> t1 -> t1) -> t
    Probable cause: `fADD' is applied to too many arguments
    In the third argument of `fIF', namely `(fADD (fPRED m) (fSUCC n))'
    In the expression: fIF (fIS_ZERO m) n (fADD (fPRED m) (fSUCC n))


I think its because in these Perl examples all functions are treated as being
of the same type (number or type of args doesn't matter), but this is
not the case in Haskell.

Is there any way to create code similar to this in Haskell?


Thanks,

Patrick

-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


------------------------------

Message: 2
Date: Thu, 04 Nov 2010 21:20:53 +0200
From: John Smith <[email protected]>
Subject: [Haskell-beginners] Re: Eliminate repetitive monad/list code
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Thank you very much, this is just the right solution.

On 04/11/2010 18:33, Tobias Brandt wrote:
> On 4 November 2010 17:19, John Smith<[email protected]>  wrote:
>> foo :: IO A
>>
>> bar :: Bool ->  A ->  Int ->  Bool ->  IO ()
>>
>> bar' a b = bar True a b False
>>
>> do
>>   x<- foo
>>   y<- foo
>>   z<- foo
>>
>>   bar x 1
>>   bar y 2
>>   bar z 3
>>
>> but I don't know what to do next. Is there some form of list comprehension
>> or combination of map and lift which will eliminate the repetition?
>
>
> mapM_ (\x ->  foo>>= flip bar x) [1,2,3]



------------------------------

Message: 3
Date: Fri, 5 Nov 2010 15:55:08 +0000
From: Tom Hobbs <[email protected]>
Subject: [Haskell-beginners] State Monads (and OpenGL)
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi all,

I have a program which implements Conway's Game Of Life.  It even has
an open GL UI which shockingly works.

I've followed a tutorial
(http://blog.mikael.johanssons.org/archive/2006/09/opengl-programming-in-haskell-a-tutorial-part-2/)
which tells how to get the open GL to animate, and according to my
sysouts, that's working to.  The problem I have comes with the fact
that between every frame I want to evolve my world but I'm really
struggling working out how to get the state stuff working.

Here's a snippet of my main function;

main = do
        world <- sequence (take (worldSize * worldSize) $ repeat rand) --
randomly generate a square world
        -- code snipped out
        displayCallback $= (display position world)  -- display method which
is called on every frame redraw
        mainLoop

Here, world is a [Int] and represents my cells.  The display function
as referenced by "displayCallback" is the function that is called back
to everytime the frame is redrawn.

I also have another function which takes in a world, updates the state
of the cells and the evolved world, thus;

evolve :: [Int] -> [Int]

I've been reading through (a lot of) tutorials on the state monad but
I'm not any closer to working out how I make the call to display take
some kind of updated world.

I've also got the following code;

type World = State [Int]

evolveS :: World [Int]
evolveS = do
              w <- get
              let w2 = evolve w
              put w2
              return w2

Which I believe to be the correct code for wrapping my [Int] in the
state monad but now I have two problems;

1) I don't know if it's correct
2) I don't know how to apply it, even if it is

Can anyone offer any advice?

Many thanks,

Tom


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 29, Issue 6
****************************************

Reply via email to