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. Re:  ghci and randomRs (Jeff C. Britton)
   2. Re:  ghci and randomRs (Francesco Ariis)
   3.  What am I reinventing here (D?niel Arat?)
   4. Re:  What am I reinventing here (Frerich Raabe)
   5. Re:  ghci and randomRs (Jeff C. Britton)


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

Message: 1
Date: Tue, 16 Sep 2014 17:43:04 +0000
From: "Jeff C. Britton" <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] ghci and randomRs
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Ok, I got it now.  I was misunderstanding how the REPL was interacting with the 
IO Monad.
I had once tried
do { g <- newStdGen; take 10 $ randomRs (1,6) g  }

but I actually needed this
do { g <- newStdGen; return . take 10 $ randomRs (1,6) g  }

Thanks,
Jeff

From: Beginners [mailto:[email protected]] On Behalf Of David 
McBride
Sent: Monday, September 15, 2014 7:27 PM
To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level 
topics related to Haskell
Subject: Re: [Haskell-beginners] ghci and randomRs

The simple answer is with do notation:
main = do
  g <- newStdGen
  print $ randomRs (1,2) g
Or without do notation, something like:
newStdGen >>= print . take 10 . randomRs (1,2)

On Mon, Sep 15, 2014 at 10:01 PM, Jeff C. Britton 
<[email protected]<mailto:[email protected]>> wrote:
I am trying to use randomRs at the ghci prompt like so

Prelude System.Random> take 10 $ (randomRs (1, 6) newStdGen)

but I get the following error
<interactive>:16:12:
    Could not deduce (RandomGen (IO StdGen))
      arising from a use of `randomRs'
    from the context (Random a, Num a)
      bound by the inferred type of it :: (Random a, Num a) => [a]
      at <interactive>:16:1-37
    In the second argument of `($)', namely
      `(randomRs (1, 6) newStdGen)'
    In the expression: take 10 $ (randomRs (1, 6) newStdGen)
    In an equation for `it': it = take 10 $ (randomRs (1, 6) newStdGen)


I have tried a variety of options, like wrapping it in a "do" or adding type 
annotations.
Nothing seems to work.



-----Original Message-----
From: Beginners 
[mailto:[email protected]<mailto:[email protected]>] On 
Behalf Of martin
Sent: Friday, September 12, 2014 10:05 AM
To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level 
topics related to Haskell
Subject: Re: [Haskell-beginners] How to add a "method" to a record

Am 09/10/2014 08:50 PM, schrieb Corentin Dupont:
> If the field "label" can be deduced from "payload", I recommend not to
> include it in your structure, because that would be redundant.
>
> Here how you could write it:
>
> data Foo pl = Foo { payload :: pl}
>
> labelInt :: Foo Int -> String
> labelInt (Foo a) = "Int payload:" ++ (show a)
>
> labelString :: Foo String -> String
> labelString (Foo a) = "String payload" ++ a
>
> You are obliged to define two separate label function, because "Foo Int" and 
> "Foo String" are two completly separate types.

This is exactly my problem: Someone will use this type an define the type of 
pl. How can I know what type she'll use?
What I'd like to express is that whoever creates a concrete type should also 
provide the proper label function.

>
>         On Wed, Sep 10, 2014 at 2:06 PM, martin 
> <[email protected]<mailto:[email protected]>> wrote:
>
>             Hello all
>
>             if I have a record like
>
>                     data Foo pl = Foo {
>                                 label :: String,
>                                 payload :: pl
>                             }
>
>             how can I create a similar type where I can populate label so it 
> is not a plain string, but a function which
>             operates on
>             payload? Something like
>
>                     label (Foo pl) = show pl
>

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140916/2958a84b/attachment-0001.html>

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

Message: 2
Date: Tue, 16 Sep 2014 21:05:49 +0200
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] ghci and randomRs
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

On Tue, Sep 16, 2014 at 05:43:04PM +0000, Jeff C. Britton wrote:
> Ok, I got it now.  I was misunderstanding how the REPL was interacting with 
> the IO Monad.
> I had once tried
> do { g <- newStdGen; take 10 $ randomRs (1,6) g  }
>
> but I actually needed this
> do { g <- newStdGen; return . take 10 $ randomRs (1,6) g  }
>
> Thanks,
> Jeff

I am sure you have already figured it out, but in case you didn't,
this works too:

?> :m +System.Random
?> g <- newStdGen
?> take 10 $ randomRs (1,6) g
[3,3,5,1,6,6,3,3,1,6]
?>

(and keeps IO actions apart from pure code)


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

Message: 3
Date: Tue, 16 Sep 2014 23:11:02 +0200
From: D?niel Arat? <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] What am I reinventing here
Message-ID:
        <CAHvKd2Knv3A=1PUyZVBaonBfsKLrS8stHrC==cbuauub1un...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hey guys,

I was working through 99 Haskell Problems a while back and while
refactoring my solution to Problem 9 I couldn't help feeling I was
reinventing something basic. My thinking was, this problem can be
generalized to letting a function recursively process arbitrary chunks
of a given data structure in several passes until the whole data is
consumed while some function is extracting some information in an
accumulator.

So I wrote a mini-typeclass like so:

class Exhaustible e where
    isEmpty :: e -> Bool
instance Exhaustible [a] where
    isEmpty = null

exhaust :: Exhaustible e => (e -> (b, e)) -> (a -> b -> a) -> a -> e -> a
exhaust f g z xs
    | isEmpty xs = z
    | otherwise  = let (val, rest) = f xs in
                   exhaust f g (g z val) rest

Now the solution to Problem 9 is easy to define in terms of "exhaust".

Then I thought, "Why am I treating empty as a special value again?",
and wrote this instead:

stabilize :: Eq a => (a -> (b, a)) -> (c -> b -> c) -> c -> a -> (c, a)
stabilize f g z d
    | d == d'   = (z', d')
    | otherwise = stabilize f g z' d'
     where (val, d') = f d
           z' = g z val

This looks both really useful and extremely basic to me, so I'm
wondering what the standard way to do the same thing might be. I
looked at Foldable + foldr but the idea there seems to be to process a
data structure _one_ element at a time.

So my question is, is there an official implementation for this
combinator I called "stabilize"? Also, any tips on improving the
definitions and comments on how situations like Problem 9 should be
approached are welcome.

Daniel


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

Message: 4
Date: Wed, 17 Sep 2014 00:02:13 +0200
From: Frerich Raabe <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] What am I reinventing here
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

On 2014-09-16 23:11, D?niel Arat? wrote:
  Then I thought, "Why am I treating empty as a special value again?",
> and wrote this instead:
> 
> stabilize :: Eq a => (a -> (b, a)) -> (c -> b -> c) -> c -> a -> (c, a)
> stabilize f g z d
>     | d == d'   = (z', d')
>     | otherwise = stabilize f g z' d'
>      where (val, d') = f d
>            z' = g z val
> 
> This looks both really useful and extremely basic to me, so I'm
> wondering what the standard way to do the same thing might be.

I think you reinvented the 'unfoldr' function here:

   unfoldr :: (b -> Maybe (a, b)) -> b -> [a]

It applies the given function to the second argument until the function
returns Nothing, which correponds to your "d == d'" guard. You could use it
to solve Problem 9 like

   pack :: Eq a => [a] -> [[a]]
   pack = unfoldr (\xs -> if null xs then Nothing else Just (span (== head xs) 
xs))

I think this looks like it does two things at once:
> I looked at Foldable + foldr but the idea there seems to be to process a
> data structure _one_ element at a time.

It's true that a fold processes a data structure one element at a time,
but you also have access to the accumulator, i.e. you can consider what
you processed so far. I think you could solve Problem 9 using a foldr
like

   pack :: Eq a => [a] -> [[a]]
   pack = foldr go []
     where
       go x acc@(a:as) = if x == head a then (x:a):as else [x]:acc
       go x _          = [[x]]

- Frerich



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

Message: 5
Date: Tue, 16 Sep 2014 23:55:16 +0000
From: "Jeff C. Britton" <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] ghci and randomRs
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Thanks Francesco.
This actually helped me.

--Jeff

-----Original Message-----
From: Beginners [mailto:[email protected]] On Behalf Of Francesco 
Ariis
Sent: Tuesday, September 16, 2014 12:06 PM
To: [email protected]
Subject: Re: [Haskell-beginners] ghci and randomRs

On Tue, Sep 16, 2014 at 05:43:04PM +0000, Jeff C. Britton wrote:
> Ok, I got it now.  I was misunderstanding how the REPL was interacting with 
> the IO Monad.
> I had once tried
> do { g <- newStdGen; take 10 $ randomRs (1,6) g  }
>
> but I actually needed this
> do { g <- newStdGen; return . take 10 $ randomRs (1,6) g  }
>
> Thanks,
> Jeff

I am sure you have already figured it out, but in case you didn't, this works 
too:

?> :m +System.Random
?> g <- newStdGen
?> take 10 $ randomRs (1,6) g
[3,3,5,1,6,6,3,3,1,6]
?>

(and keeps IO actions apart from pure code) 
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 75, Issue 11
*****************************************

Reply via email to