Given the following function, I receive a parse error from GHC.
has_empty_string :: [String] -> Bool
has_empty_string col =
not foldl (\acc now -> if and [acc == True, now == ""] then False else True) True col
I’ve checked a variety of tutorials and I don’t see the error.
What is more confusing is that GHC takes it, when I prepend let to the definition.
Rewritten to use any_null.
I have a suspicion that the error is actually elsewhere…
module Main where
import System.Cmd
import System.Directory
import System.Environment
import System.IO
import System.Process
import Text.Regex.Posix
import Data.CSV.Enumerator
import Data.Spreadsheet
import Data.Char (isSpace)
--------------------------------------------------
slurp :: String -> IO String
slurp path = do
withFile path ReadMode (\handle -> do
contents <- hGetContents handle
last contents `seq` return contents )
--------------------------------------------------
has_empty_string :: [String] -> Bool
has_empty_string col =
any null
main :: IO ()
main = do
[filename] <- getArgs
raw_data <- slurp filename
let csv_data = fromString '"' ',' raw_data
-- checking to see if the 5th column is an empty string
content_hashes = get_hashrow csv_data
where get_hashrow = map (\row -> row !! 5) csv_data
is_good_csv = has_empty_hash content_hashes
putStrLn $ show csv_data
When I try to compile your code with
ghcI get a type error, not a parse error. That error is because you’re trying to callnotwithfoldlas its first argument and(\ acc now -> ...)as the second argument. You need to add a$afternotor wrap the call tofoldlin parentheses.You probably got the parser error you mentioned in ghci. In ghci you’re not allowed to write definitions the way you would in a file. You’re only allowed to enter expressions (which will then be evaluated and the result printed – or executed if it’s an IO action) or anything you could write inside an IOdoblock (i.e.foo <- barandlet foo = bar).So in ghci you can define variables and functions only by using
letand you can’t define types at all.The parse error you get is actually caused by
mainwhere you try to define local variables withoutlet. Just add ´letbeforecontent_hashes = …andis_good_csv = …` and the parse error will disappear.