Haskell newbie here so please excuse if the question is too simple. Its just that I dont seem to be able to get a handle on why some things are ok in a file, but not in GHCI. For example, I have the following lines in a file:
showLst :: [[Int]] -> IO ()
showLst = putStrLn . unlines . map show
This takes a m x n array and prints the result on the screen. A very convenient function. However, when I am doing a quick check on GHCI and I want the same functionallity, I try to define the same function in GHCI like so:
>> let showLst = putStrLn . unlines . map show
>> showLst [[1,2,3], [4,5,6], [7,8,9]]
I get a type error. So I try several variations:
>> (showLst [[1,2,3], [4,5,6], [7,8,9]]) :: IO ()
>> (showLst:: [[Int]] -> IO ()) [[1,2,3], [4,5,6], [7,8,9]]
>> (showLst [[1,2,3], [4,5,6], [7,8,9]]) :: [[Int]] -> IO () -- which us wrong anyway
>> showLst [[1,2,3], [4,5,6], [7,8,9]] :: [[Int]] -> IO () -- which is also wrong
etc and they all fail. This seems to be a very simple thing to do, but not sure why I am finding this to be so difficult. I must be missing something that is really fundamental. Can someone please let me know what I am doing wong ?
GHCi is the interactive interface to GHC. From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.
Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)
This library offers interfaces which mediate interactions between the ghci interactive shell and iserv , GHC's out-of-process interpreter backend.
The problem is that due to monomorphism restriction GHCi defaults type of your list element to ()
:
Prelude> let showLst = putStrLn . unlines . map show
Prelude> :t showLst
showLst :: [()] -> IO ()
You can disable this restriction and get general type:
Prelude> :set -XNoMonomorphismRestriction
Prelude> let showLst = putStrLn . unlines . map show
Prelude> :t showLst
showLst :: Show a => [a] -> IO ()
Or you can specify desired type manually:
Prelude> let showLst = putStrLn . unlines . map show :: [[Int]] -> IO ()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With