0
votes

This is what I have so far, I am pretty sure the function itself is correct but I am confused on what to do in the main = do block. The error I am getting is that the function wants an integer but I am giving it a string instead. I am assuming it has to do with the getLine part and the fact I am not checking to see if the entered number is an int.

factors :: Int -> [Int]
factors n = [x | x <- [1..n], mod n x == 0]
primeFactors :: Int -> [Int]
primeFactors n = [x | x <- [1..n], mod n x == 0, isPrime x]
isPrime x = (length (factors x)) == 2
main = do
  putStrLn "Enter a number."
  number <- getLine
  let x = factors number
  putStrLn x

The errors I get when I run this code:

primeFactor.hs:9:19: error:

    • Couldn't match type ‘[Char]’ with ‘Int’
      Expected type: Int
        Actual type: String
    • In the first argument of ‘factors’, namely ‘number’
      In the expression: factors number
      In an equation for ‘x’: x = factors number
      

primeFactor.hs:10:12: error:

    • Couldn't match type ‘Int’ with ‘Char’
      Expected type: String
        Actual type: [Int]
    • In the first argument of ‘putStrLn’, namely ‘x’
      In a stmt of a 'do' block: putStrLn x
      In the expression:
        do { putStrLn "Enter a number.";
             number <- getLine;
             let x = factors number;
             putStrLn x }
    
1
Please include actual error messages in your question, rather than presenting paraphrased or abridged versions. They really are useful. The error message you ask about in the comment is because putStrLn wants a String, and x is a list of Ints, not a String.amalloy
Thanks, I am fairly new to this site, Ill keep this in mind for future questionsTyler

1 Answers

3
votes
let x = factors $ (read number :: Int)
print x

OR

number <- readLn

(thanks @Daniel)

This should do the work.