1

I want to get a random prime from a prime array name "primes". So I want to generate a random integer first, and then get the prime at that Integer position.

Here is my code:

getPrime :: IO Integer
getPrime =  do 
        pos <- getStdRandom (randomR (10, 100))
        return (primes !! pos)

(primes already exists and works well.)

But when I load this code, it says "parse error on input `return'"

Can anyone help me about this? Thx!!!!!!!!!!!!!!!

  • After replacing all the undefined names with `undefined` your code loads without error for me (GHC 7.6.3). So, the problem may be with the code that you did not paste. – ntc2 Oct 12 '13 at 09:02

2 Answers2

3

The problem must be with whitespace. You must have mixed tabs with spaces in the indentation. To protect yourself from that kind of situation it's conventional in Haskell to only indent with spaces.

Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
2

The problem isn't in the given code as the following code works without trouble:

import System.Random

primes = [1..100]

getPrime :: IO Integer
getPrime = do
      pos <- getStdRandom (randomR (10, 100))
      return (primes !! pos)

main = getPrime

I would suggest starting with this and extending it piece by piece to see where the problem is.