I have this code that I tried to create to count amount of records and print them, I cant seem to get it working I constantly get errors about a function reportReg being applied to one argument but its type [String] having none.
report :: [[String]] -> String -> [String]
report (x:xs) typ = do
case typ of
"registrations" -> reportReg (map head xs)
"completions" -> reportReg (map head xs)
reportReg :: [String]
reportReg [x] = do
print x
print 1
reportReg (x:xs) = do
let count = instances x (x:xs)
print x
print count
let newlist = filter (==x) (x:xs)
reportReg newlist
instances::String->[String]->Int
instances x [] = 0
instances x (y:ys)
| x==y = 1+(instances x ys)
| otherwise = instances x ys
Also, is there an easier way to do this?
Problem:
You’ve given
reportRega type of list of string:This is simply a value, or a function of 0 arguments. That explains the error you were getting — trying to give it an argument, but it takes none.
Solutions:
It looks like you want to do IO actions in
reportReg, so you should change the type annotation:— or —
Problem:
report‘s return type is wrong. It has to be the same as that ofreportReg. ButreportReg :: String -> IO (), whereasreport :: [[String]] -> String -> [String]!A couple possible solutions:
reportReg, so that its type is[String] -> [String]. I’d strongly suggest doing this — IO in any language is always a pain, but the cool thing about Haskell is that it makes you feel the pain — thereby giving you an incentive to avoid IO as much as possible!reportto[[String]] -> String -> IO ()Lazy man’s solution:
I copied your code into a text file, removed the annotations (making no other changes), and loaded it into
ghci:It works — Haskell infers the types! But it may not do what you want.