0

I am trying to create an Int list of random numbers which is made by taking 62 numbers from another list of 1000 random numbers starting from a random index which is one of the inputs to the function. this is what I currently have:

num :: [Int]->Int->[Int]
num list index = numGenerator list index 62
numGenerator :: [Int]->Int->Int->[Int]
numGenerator list index counter 
    | counter == 0 = []
    | otherwise = list !! index :: numGenerator((list), (index + 1),(counter - 1))
 main = do
    let numGen =  num 100 

I'm new to haskell so please let me know what I'm doing wrong thanks!

1 Answers1

4

The main problems are:

  • The operator :: is used to annotate expressions with type signatures. You want the operator : to prepend an element to a list.
  • In Haskell, multiparameter functions are not called like f(a,b,c). They are called like f a b c instead, with parentheses added where necessary. Your recursive call to numGenerator needs to be fixed to match the correct syntax.
  • Indentation is important -- main must start in the same column as numGenerator.
  • In main, you call num 100, but num takes a list and an integer, not just an integer.
  • In main, your do-block ends with a let statement. That's not allowed. You need to end it with an IO action, like printing a result or something.

Fixing that up, you'll get something like:

num :: [Int] -> Int -> [Int]
num list index = numGenerator list index 62

numGenerator :: [Int] -> Int -> Int -> [Int]
numGenerator list index counter
    | counter == 0 = []
    | otherwise = list !! index : numGenerator list (index + 1) (counter - 1)

main = do
  let numGen =  num [1..1000] 100
  print numGen

which seems to work, though the result isn't very random, since my test supplies the decidedly non-random list [1..1000].

The mistakes you're making indicate that you still haven't mastered many of the basics of writing Haskell programs. Hopefully, you've already seen this question and are working through the resources in the answers.

You may also find it helpful to install and use a Haskell IDE. I hear that Visual Studio Code is easy to use and works well. An IDE will flag syntax errors early, so you can fix them as you go, instead of writing multiple functions and then discovering you have dozens of error messages you can't understand and somehow need to work through.

However, I think it's more important to work through more tutorials than get a Haskell IDE up and running, if you have to prioritize one or the other.

K. A. Buhr
  • 45,621
  • 3
  • 45
  • 71