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.  About 'Curry' (???)
   2. Re:  About 'Curry' (Tobias Brandt)
   3. Re:  Relying on memoization (Russ Abbott)
   4. Re:  Relying on memoization (Daniel Fischer)
   5.  state and randomness, Rand, RandT (Amy de Buitl?ir)
   6. Re:  state and randomness, Rand, RandT (Brent Yorgey)
   7. Re:  state and randomness, Rand, RandT (Amy de Buitl?ir)


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

Message: 1
Date: Fri, 10 Dec 2010 19:59:31 +0800
From: ??? <[email protected]>
Subject: [Haskell-beginners] About 'Curry'
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

pl a b = a ++ b

pl a = a ++

The second definition cannot be accept by neigher ghci nor ghc.
What's the problem?
(ghc 6.12.3)

-- 
----------------
WU XingBo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20101210/527bab0a/attachment-0001.htm>

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

Message: 2
Date: Fri, 10 Dec 2010 13:05:42 +0100
From: Tobias Brandt <[email protected]>
Subject: Re: [Haskell-beginners] About 'Curry'
To: ??? <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

To use an operator as a function you must use the section notation:

pl a = (a ++)

On 10 December 2010 12:59, ??? <[email protected]> wrote:
> pl a b = a ++ b
>
> pl a = a ++
>
> The second definition cannot be accept by neigher ghci nor ghc.
> What's the problem?
>
> (ghc 6.12.3)
>
> --
> ----------------
> WU XingBo
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>



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

Message: 3
Date: Fri, 10 Dec 2010 09:40:39 -0800
From: Russ Abbott <[email protected]>
Subject: Re: [Haskell-beginners] Relying on memoization
To: Daniel Fischer <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

After writing the previous post, it struck me that memoization could be done
at the function level. If each function had an associated Map <Arguments>
<Result>, lookup would be much simpler and more localized. One could then
decide on a function-by-function basis which maps to expand.
*
-- Russ *



On Fri, Dec 10, 2010 at 1:16 AM, Daniel Fischer <
[email protected]> wrote:

>
>
> On Fri, Dec 10, 2010 at 5:39 AM, Russ Abbott <[email protected]>wrote:
>
>> (Is that even the right term?)
>>
>
> Yes.
>
>
>> Let's suppose I have a fairly complex computation to perform on a data
>> structure. Normally I would cache the computed value in a field of the
>> structure. The next time I need it I don't have to compute it again. (Of
>> course if the structure changes, I have to recompute the value and store it
>> again.)
>>
>> In Haskell is it the case that caching of this sort is redundant since
>> Haskell does it for me?  That is, if I call
>>
>> f someStructure
>>
>>
>> multiple times at different places in my code and if both "f" and
>> "someStructure" refer to the same things each time, can I rely on Haskell
>> not to perform the computation multiple times but to look up the result it
>> previously computed?
>>
>
> No, you can't rely on that, and in general f someStructure will be computed
> multiple times.
> If two "f someStructure" appear in the same expression, there's a chance
> they will be shared, but you can't rely on it. It's very hard to determine
> when sharing common subexpressions is beneficial and when it's detrimental,
> so at least in GHC, common subexpression elimination is not done much. If
> you want something to be shared, give it a name in the enclosing scope, the
> value that name is bound to will be computed only once (unless polymorphism
> prevents sharing and forces the value to be recomputed).
>
>
>>
>> Is there some limit to that? If every computation is stored, doesn't that
>> create a very large collection of stored results?
>>
>
> Right, and apart from needing a lot of memory, storing everything makes
> finding the cached result slower than recomputing it in many cases.
>
>
>>  *
>> -- Russ *
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20101210/91428d84/attachment-0001.htm>

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

Message: 4
Date: Fri, 10 Dec 2010 21:29:01 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Relying on memoization
To: [email protected]
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

On Fri, Dec 10, 2010 at 6:40 PM, Russ Abbott <[email protected]> wrote:

> After writing the previous post, it struck me that memoization could be
> done at the function level. If each function had an associated Map
> <Arguments> <Result>, lookup would be much simpler and more localized. One
> could then decide on a function-by-function basis which maps to expand.
> *
> -- Russ *
>
>
There are packages on hackage to memoise functions, e.g.
http://hackage.haskell.org/package/data-memocombinators

You have to decide yourself which functions to memoise, there are no good
heuristics for the compiler to decide, as far as I know.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20101210/6da14ff0/attachment-0001.htm>

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

Message: 5
Date: Sat, 11 Dec 2010 01:48:36 +0000
From: Amy de Buitl?ir <[email protected]>
Subject: [Haskell-beginners] state and randomness, Rand, RandT
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

I'm experimenting with different ways of working with both state and
randomness at the same time. I put together this simple little example
as a starting point, where the state in question is a list of Ints,
and I want to be able to push a random number onto the stack.

{-# LANGUAGE PackageImports #-}

import "mtl" Control.Monad.State
import Control.Monad.Random

type Stack = [Int]

-- push a random number onto the stack
pushRandom:: State (Stack, StdGen) ()
pushRandom = do
     (xs,gen) <- get
     (r,gen') <- return $ randomR (1,100) gen
     put(r:xs,gen')

That implementation works just fine.

GHCi> execState pushRandom ([5,4,3,2,1], mkStdGen 0)
([46,5,4,3,2,1],40014 40692)

But... I think it would be nicer to use Rand. Unfortunately, I can't
figure out how to write the correct type signature. (Compiler message
is: `Rand g' is not applied to enough type arguments Expected kind
`*', but `Rand g' has kind `* -> *').

pushRandom2 :: (RandomGen g) => State (Stack, Rand g) ()
pushRandom2 = do
  (xs,gen) <- get
  (x,gen') <- getRandomR (0,100) gen
  put (1:xs,gen)

And I'd really like to try using RandT, because this seems like the
right situation for it. This compiles just fine.

pushRandom3 :: (RandomGen g) => RandT g (State Stack) ()
pushRandom3 = do
  xs <- get
  r <- getRandomR (1,100)
  put (r:xs)

But I don't know if it works, because I can't figure out the magic
incantation to get it to run!

GHCi> evalRandT $ pushRandom3 (mkStdGen 0) [5,4,3,2,1]

<interactive>:1:12:
    Couldn't match expected type `StdGen -> [t] -> RandT g m a'
           against inferred type `RandT g1 (State Stack) ()'
    In the second argument of `($)', namely
        `pushRandom3 (mkStdGen 0) [5, 4, 3, 2, ....]'
    In the expression:
          evalRandT $ pushRandom3 (mkStdGen 0) [5, 4, 3, 2, ....]
    In the definition of `it':
        it = evalRandT $ pushRandom3 (mkStdGen 0) [5, 4, 3, ....]

To summarise:

Q1: How can I fix pushRandom2?

Q2: How can I run pushRandom3?

Thank you in advance for anyone who can help.

Amy



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

Message: 6
Date: Fri, 10 Dec 2010 21:45:34 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] state and randomness, Rand, RandT
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

On Sat, Dec 11, 2010 at 01:48:36AM +0000, Amy de Buitl?ir wrote:
> 
> And I'd really like to try using RandT, because this seems like the
> right situation for it. This compiles just fine.
> 
> pushRandom3 :: (RandomGen g) => RandT g (State Stack) ()
> pushRandom3 = do
>   xs <- get
>   r <- getRandomR (1,100)
>   put (r:xs)

Indeed, this is the right way to do it!

> 
> But I don't know if it works, because I can't figure out the magic
> incantation to get it to run!

Let's look at the type of evalRandT:

*Main Control.Monad.Random> :t evalRandT
evalRandT :: (Monad m, RandomGen g) => RandT g m a -> g -> m a

So we have:

*Main Control.Monad.Random> :t evalRandT pushRandom3 
evalRandT pushRandom3 :: (RandomGen g) => g -> State Stack ()

Let's pass it a generator:

*Main Control.Monad.Random> :t evalRandT pushRandom3 (mkStdGen 0)
evalRandT pushRandom3 (mkStdGen 0) :: State Stack ()

(Note, if you use mkStdGen 0 you will get the same random numbers
every time you run your program; instead you could use getStdGen to
get one based on the system clock or something like that.)
  
Now we have a State computation, so let's run it:

*Main Control.Monad.Random> :t execState (evalRandT pushRandom3 (mkStdGen 0))
execState (evalRandT pushRandom3 (mkStdGen 0)) :: Stack -> Stack
*Main Control.Monad.Random> :t execState (evalRandT pushRandom3 (mkStdGen 0)) 
[1,2,3,4,5]
execState (evalRandT pushRandom3 (mkStdGen 0)) [1,2,3,4,5] :: Stack

-Brent



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

Message: 7
Date: Sat, 11 Dec 2010 02:55:02 +0000
From: Amy de Buitl?ir <[email protected]>
Subject: Re: [Haskell-beginners] state and randomness, Rand, RandT
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On 11 December 2010 02:45, Brent Yorgey <[email protected]> wrote:
> Let's look at the type of...

Your explanation was very clear. Thank you so much, Brent.



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

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


End of Beginners Digest, Vol 30, Issue 14
*****************************************

Reply via email to