Haskell: How to use random integer from randomRIO in a function that returns a boolean

Noob Saibot

I am new to Haskell and i'm having a problem with using the IO Int from randomRIO function. My goal is to get a random Int value, say r, and to return True if r < x or false otherwise, but i don't know how to do it.

my function should look like:

randomCompare :: Int->Bool

randomCompare x

  | x < r = True -- somehow i want to r <- randomRIO(start,end) 

  | otherwise = False                

I know there is a designed intention with keeping IO vals in context for purity etc.. but i don't see why using a random number for a Boolean function should be "bad".

Thanks.

V. Semeria
import System.Random(randomIO)

randomCompare :: Int -> IO Bool
randomCompare x = do
  r <- randomIO
  return $ x < r

IO is neither good nor bad, it just declares that your function has side effects. Here the side effect is modifying the state of the global random number generator, so that a subsequent call to randomIO will give another number (it wouldn't be random if it was constant !).

IO does force all calling functions to be IO too (the ones that want to use the IO Bool). However, if a calling function is IO only by consuming this IO Bool, if it has no other side effects, then you can separate it as a pure function f :: Bool -> SomeType and functorially apply it on the IO, ie

f <$> randomCompare i

So the IO monad only costs you to replace the ordinary function call $ by the functorial fmap, also noted <$>. Is it so much longer to type ?

If you absolutely want to leave the IO monad (why ?), you can also draw all the random values you need first, store them in a list, then apply pure functions on that list.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use retryWhen with a function that returns a Boolean?

From Dev

Haskell - How to use a function that returns 'Maybe Int' as an argument to another function?

From Dev

How to generate an infinite list of random numbers using randomRIO?

From Dev

How to return a random integer from a two parameters function

From Dev

Maxima: define a function that returns a random integer in a range, such that the value is distinct from another value or a list of other values

From Dev

Boolean variable returns as string from javascript function

From Dev

Haskell random generator wrapped into a function always returns the same value

From Dev

Haskell random generator wrapped into a function always returns the same value

From Dev

how to use random function in python?

From Dev

Function returns boolean not string

From Dev

Haskell: how to use case function

From Dev

how to return an integer value from function without passing any argument in haskell

From Dev

Python Turtle: How to use the write function to join an integer and a string with the integer coming from a list

From Dev

Haskell function returning Integer

From Dev

Haskell : No instance for Random Point arising from a use of genRandom

From Dev

How to implement a function that returns boolean and changes one of its parameters?

From Dev

How to return boolean value from a function in Swift

From Dev

How to read a boolean from database (use cursor)?

From Dev

Function returns always Boolean False

From Dev

Haskell - From generic to Integer | No instance for (RealFrac Integer) arising from a use of ‘floor’

From Dev

How to use integer literals and arithmetic within Haskell data kinds

From Dev

Assigning Boolean returns Boolean, but assigning integer does not. Why?

From Dev

How to properly use Haskell's `parseTimeM` function?

From Dev

How to use multiple 'commands' in the same function in Haskell

From Dev

How to properly use Haskell's `parseTimeM` function?

From Dev

How to reverse an integer in haskell?

From Dev

Linq How to perform Subquery with max function that returns a primitive value such as an integer?

From Dev

Integer square root function in Haskell

From Dev

Haskell checkers - how to write a function that returns a list of possible jumps

Related Related

  1. 1

    How to use retryWhen with a function that returns a Boolean?

  2. 2

    Haskell - How to use a function that returns 'Maybe Int' as an argument to another function?

  3. 3

    How to generate an infinite list of random numbers using randomRIO?

  4. 4

    How to return a random integer from a two parameters function

  5. 5

    Maxima: define a function that returns a random integer in a range, such that the value is distinct from another value or a list of other values

  6. 6

    Boolean variable returns as string from javascript function

  7. 7

    Haskell random generator wrapped into a function always returns the same value

  8. 8

    Haskell random generator wrapped into a function always returns the same value

  9. 9

    how to use random function in python?

  10. 10

    Function returns boolean not string

  11. 11

    Haskell: how to use case function

  12. 12

    how to return an integer value from function without passing any argument in haskell

  13. 13

    Python Turtle: How to use the write function to join an integer and a string with the integer coming from a list

  14. 14

    Haskell function returning Integer

  15. 15

    Haskell : No instance for Random Point arising from a use of genRandom

  16. 16

    How to implement a function that returns boolean and changes one of its parameters?

  17. 17

    How to return boolean value from a function in Swift

  18. 18

    How to read a boolean from database (use cursor)?

  19. 19

    Function returns always Boolean False

  20. 20

    Haskell - From generic to Integer | No instance for (RealFrac Integer) arising from a use of ‘floor’

  21. 21

    How to use integer literals and arithmetic within Haskell data kinds

  22. 22

    Assigning Boolean returns Boolean, but assigning integer does not. Why?

  23. 23

    How to properly use Haskell's `parseTimeM` function?

  24. 24

    How to use multiple 'commands' in the same function in Haskell

  25. 25

    How to properly use Haskell's `parseTimeM` function?

  26. 26

    How to reverse an integer in haskell?

  27. 27

    Linq How to perform Subquery with max function that returns a primitive value such as an integer?

  28. 28

    Integer square root function in Haskell

  29. 29

    Haskell checkers - how to write a function that returns a list of possible jumps

HotTag

Archive