[Haskell-cafe] Random question

John Van Enk vanenkj at gmail.com
Wed Sep 24 17:13:36 EDT 2008


And the one liner:
(rand 1 10) >>= return . (\v -> take v [1..10])

On Wed, Sep 24, 2008 at 5:10 PM, Lev Walkin <vlm at lionet.info> wrote:

> forgot return, of course:
>
> > myTake :: IO [Int]
> > myTake = do
> >     n <- rand 1 10
> >     return $ take n [1..10]
>
>
> Lev Walkin wrote:
>
>> Iain Barnett wrote:
>>
>>> Hi,
>>>
>>> I have a function, that produces a random number between two given
>>> numbers
>>>
>>> rand :: Int -> Int -> IO Int
>>> rand low high = getStdRandom (randomR (low,high))
>>>
>>>
>>> (Naively) I'd like to write something like
>>>
>>> take (rand 1 10 ) [1..10]
>>>
>>> and see [1,2,3,4] ... or anything but nasty type-error messages.
>>>
>>
>> myTake :: IO [Int]
>> myTake = do
>>    n <- rand 1 10
>>    take n [1..10]
>>
>> or
>>
>> myTake = rand 1 10 >>= \n -> take n [1..10]
>>
>> or
>>
>> myTake = rand 1 10 >>= flip take [1..10]
>>
>>  I'm reading about 6 tutorials on monads simultaneously but still can't
>>> crack this simple task, and won't pain you with all the permutations of code
>>> I've already tried. It's a lot, and it ain't pretty.
>>>
>>> Would anyone be able to break away from C/C++ vs Haskell to help? Just a
>>> point in the right direction or a good doc to read, anything that helps will
>>> be much appreciated.
>>>
>>
>>
>> Monad enlightenment happens after 7'th monad tutorial. Verified by me
>> and a few of my friends.
>>
>>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



-- 
/jve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080924/ddab0426/attachment.htm


More information about the Haskell-Cafe mailing list