Error trying to call putStrLn in function

I am trying to put a call to the print function in a Haskell function.

(simple debug message).

Below is my code and error message from the compiler (GHC 6.10).

I do not quite understand why it mixes a call putStrand an empty array.

An empty array is the return value for this particular case (for now, the printout message is actually just a stub).

Any idea why this doesn't work?

My code is:

isAFactor :: Integer -> Integer -> Bool 
isAFactor xy = x 'mod' y == 0

findFactors :: Integer -> Integer -> [Integer]
findFactors counter num = 
    let quotient = div num 2
    in
        if (counter> quotient)
            then do
                putStrLn ("factorList is:" ++ show quotient) (*** Line 10 ***)
                []
        else if (isAFactor num counter)
            then [counter] ++ [quotient] ++ findFactors (counter + 1) num
        else
            findFactors (counter + 1) num

Error from ghc

    test.hs: 10: 4:
    Couldn't match expected type '[a] -> [Integer]'
           against inferred type 'IO ()'
    In the expression:
        putStrLn ("factorList is:" ++ show quotient) []
    In the expression:
        do putStrLn ("factorList is:" ++ show quotient) []
    In the expression:
        if (counter> quotient) then
            do putStrLn ("factorList is:" ++ show quotient) []
        else
            if (isAFactor num counter) then
                  [counter] ++ [quotient] ++ findFactors (counter + 1) num
            else
                findFactors (counter + 1) num
+5
4

, Haskell pure. , - , .

, . Debug.Trace. trace :: String -> a -> a. :

import Debug.Trace

isAFactor :: Integer -> Integer -> Bool 
isAFactor x y = x `mod` y == 0

findFactors :: Integer -> Integer -> [Integer]
findFactors counter num = 
    let quotient = div num 2
    in
        if(counter >  quotient)
                then trace ("factorList is: " ++ show quotient) [] 
        else if(isAFactor num counter)
            then [counter] ++ [quotient] ++ findFactors (counter + 1) num
        else
            findFactors (counter + 1) num

:

Haskell lazy. , . , , ( ).

haskell - , , -. trace "" . haskells () QuickCheck, .

+21

Jonas , findFactors. , .

, n, 1 n/2, , n , .

( , ):

findFactors :: Integer -> Integer -> [Integer]
findFactors counter num = 
    let quotient = div num 2
    in
        if(counter >  quotient)
            then []
        else if(isAFactor num counter)
            then [counter] ++ findFactors (counter + 1) num
        else
            findFactors (counter + 1) num

, :

findFactors :: Integer -> Integer -> [Integer]
findFactors counter num
  | counter > div num 2 = []
  | otherwise = if num `isAFactor` counter 
                then counter:findFactors (counter+1) num
                else findFactors (counter + 1) num

, , . -, , findFactors, n/2 ( ghc -O2, , ). -, . -, .

- 1 n/2 , n. Haskell:

findFactors :: Integer -> [Integer]
findFactors num = filter (isAFactor num) [1..(num `div` 2)]

, , . Haskell n/2 , .

+9

, .

Couldn't match expected type `[a] -> [Integer]'
       against inferred type `IO ()'
In the expression:
    putStrLn ("factorList is  : " ++ show quotient) []

" "; .

Haskell , . - "putStrLn". ": t putStrLn" GHCi, :

putStrLn :: String -> IO ()

, putStrLn - , " IO", . "putStrLn" , , "putStrLn (stuff)" "IO()". " " .

, , . , "putStrLn (stuff)" , , , "[ a]" ( -, , ). , "[ ]". "putStrLn (stuff)" , "[]" , "[a] → [Integer]". .

, , .

" " Foo " " Bar ", , , Haskell, . , . , - , .

+8

, IO Haskell , , do, ( ) . IO, putStrLn. [] do do, putStrLn; , , , . , , , , (.. return []). , , .

- , IO IO _; do , [Integer], , .

, Haskell - IO, IO, , . .. do - , IO _, , , .. ( "" ", IO .)

+3

All Articles