Sto provando a mettere una chiamata di funzione 'stampa' in una funzione haskell.haskell: errore nel tentativo di chiamare putStrLn nella funzione
(un semplice messaggio di debug).
Di seguito è riportato il mio codice e il messaggio di errore del compilatore (ghc 6.10).
Non capisco perché sta interrompendo la chiamata di putstr e l'array vuoto.
L'array vuoto è il valore restituito per quel caso particolare (il messaggio di stampa è in realtà solo uno stub per ora).
Qualche idea sul perché questo non funziona?
Grazie
My Code:
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 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
errore da 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
Credo che questo sia dovuto al fatto che findFactors è una funzione pura, quindi non può avere effetti collaterali (come IO). Questo è Haskell. Non ho idea di come debugare la tua funzione, però. In C++ uso sempre il debug-logging. –
"fare" non significa quello che pensi che significhi. È solo lo zucchero di sintassi per il sequenziamento di monade; in realtà non puoi "fare" nulla in Haskell. :-) – ShreevatsaR