Reading lines and reversing the text with Haskell - string

My program should do the following:
Read lines from a file
For each line, reverse the text of each word ("bat cat hat" becomes "tab tac tah")
Print out the the reverse text
But it doesn't work and being a Haskell-newbie, I can't understand the errors.
Here is my code:
main = do
content <- readFile "test.txt"
let linesList = lines content
reverseLines
reverseWords :: String -> String
reverseWords = unwords . map reverse . words
reverseLines :: [String] -> IO ()
reverseLines inputList = do
if null inputList
then return ()
else do
line <- inputList!!0
if null line
then return ()
else do
putStrLn $ reverseWords line
reverseLines . tail inputList
And my errors:
readlines.hs:4:9:
Couldn't match expected type `IO b0'
with actual type `[String] -> IO ()'
In a stmt of a 'do' block: reverseLines
In the expression:
do { content <- readFile "test.txt";
let linesList = lines content;
reverseLines }
In an equation for `main':
main
= do { content <- readFile "test.txt";
let linesList = ...;
reverseLines }
readlines.hs:14:25:
Couldn't match type `[Char]' with `IO [Char]'
Expected type: [IO [Char]]
Actual type: [String]
In the first argument of `(!!)', namely `inputList'
In a stmt of a 'do' block: line <- inputList !! 0
In the expression:
do { line <- inputList !! 0;
if null line then
return ()
else
do { putStrLn $ reverseWords line;
.... } }
readlines.hs:19:33:
Couldn't match expected type `IO ()' with actual type `a0 -> IO ()'
In a stmt of a 'do' block: reverseLines . tail inputList
In the expression:
do { putStrLn $ reverseWords line;
reverseLines . tail inputList }
In a stmt of a 'do' block:
if null line then
return ()
else
do { putStrLn $ reverseWords line;
reverseLines . tail inputList }
readlines.hs:19:48:
Couldn't match expected type `a0 -> [String]'
with actual type `[String]'
In the return type of a call of `tail'
Probable cause: `tail' is applied to too many arguments
In the second argument of `(.)', namely `tail inputList'
In a stmt of a 'do' block: reverseLines . tail inputList

Firstly, your last line in your main function is of type [String] -> IO (), and you need it to be IO (), so you actually need to pass it the list of strings. Here is one way to do what you are requesting:
main :: IO ()
main = do
content <- readFile "test.txt"
let linesList = lines content
reverseLines linesList
reverseWords :: String -> String
reverseWords = unwords . map reverse . words
reverseLines :: [String] -> IO ()
reverseLines = mapM_ (putStrLn . reverseWords)
And here is a recursive version akin to what you wanted to do in your version
reverseLines2 :: [String] -> IO ()
reverseLines2 [] = return ()
reverseLines2 (x:xs) = do
putStrLn (reverseWords x)
reverseLines2 xs
I've also fixed your version with minimal changes, but bear in mind that this is not really the Haskell way to do things. Go with one of the options I provided above.
main = do
content <- readFile "test.txt"
let linesList = lines content
reverseLines linesList
reverseWords :: String -> String
reverseWords = unwords . map reverse . words
reverseLines :: [String] -> IO ()
reverseLines inputList = do
if null inputList
then return ()
else do
let line = head inputList
if null line
then return ()
else do
putStrLn $ reverseWords line
reverseLines $ tail inputList

Related

Haskell ::Couldn't match type `Char' with `[Char]'

I want a program that takes lines and prints reverse when it encounters an empty line.
This is my code.
back :: IO()
back = do
line <- getLine
if (not $ null line) then do
mapM_ putStrLn (reverse line)
else return()
When I try to run this it gives an error.
* Couldn't match type `Char' with `[Char]'
Expected: [String]
Actual: [Char]
* In the second argument of `mapM_', namely `(reverse line)'
In a stmt of a 'do' block: mapM_ putStrLn (reverse line)
In the expression: do mapM_ putStrLn (reverse line)
|
6 | mapM_ putStrLn(reverse line)
| ^^^^^^^^^^^^
What is going wrong here?
line is a String. Since you use mapM_ putStrLn, it expects a list of strings, so mapM_ putStrLn :: [String] -> IO ().
It is not entirely clear if you want to reverse each line, or the lines itself. In case of the former, you can work with:
back :: IO()
back = do
line <- getLine
if not (null line) then do
putStrLn (reverse line)
back
else return()
in case of the latter, you can use recursion, and first call back recursively, and then print the line:
back :: IO()
back = do
line <- getLine
if not (null line) then do
back
putStrLn line
else return()

Type errors when using IO action in Haskell

I just started learning Haskell and now got stuck in dealing with IO action.
Here's the code.
parseDnsMessage :: BG.BitGet DnsMessage
recQuery :: BS.ByteString -> String -> IO BS.ByteString
resolveName :: [Word8] -> [Word8] -> BS.ByteString -> String
resolveName qname name bstr = do
let newbstr = BSL.toStrict $ replace (BS.pack qname) (BS.pack name) bstr
retbstr <- recQuery newbstr (head rootServers4)
let msg = BG.runBitGet retbstr parseDnsMessage
case msg of
Right m -> (intercalate "." $ map show (rdata $ head $ answer $ m))
---Error Message---
Couldn't match expected type ‘[BSI.ByteString]’
with actual type ‘IO BSI.ByteString’
In a stmt of a 'do' block:
retbstr <- recQuery newbstr (head rootServers4)
In the expression:
do { let newbstr
= BSL.toStrict $ replace (BS.pack qname) (BS.pack name) bstr;
retbstr <- recQuery newbstr (head rootServers4);
let msg = BG.runBitGet retbstr parseDnsMessage;
case msg of {
Right m
-> (intercalate "." $ map show (rdata $ head $ answer $ m)) } }
I just want to retrieve BS.ByteString from the recQuery IO action.
How can I fix this?
The problem is your resolveName should return IO String, not String. This is because it is operating in the IO monad, i.e. you are chaining IO actions in it, so it must return IO.

Haskell --- error using SplitOn ","

getLines = liftM lines . readFile
main = do
argv <- getArgs
name <- getProgName
if not (null argv)
then do
let file = head argv
list <- getLines file
let olist = mergesort (<=) list
let splitter = splitOn "," olist
loop olist
else hPutStr stderr $ "usage: " ++ name ++ " filename"
loop a = do
line <- getLine
case line of
"help" -> putStrLn "print - prints list in alphabetical order\n\
\quit - exits program"
"print" -> do putStrLn "[print]"
mapM_ putStrLn a
putStr "\n"
"quit" -> do putStrLn "[quit]"
exitSuccess
_ -> putStrLn "invalid command"
loop a
I'm getting this error:
Couldn't match type '[Char]' with `Char'
Expected type: [Char]
Actual type: [String]
any tips?
You need to use single quotes for char constants.
See this
let splitter = splitOn ',' olist

Why can't I compare result of lookup to Nothing in Haskell?

I have the following code:
import System.Environment
import System.Directory
import System.IO
import Data.List
dispatch :: [(String, [String] -> IO ())]
dispatch = [ ("add", add)
, ("view", view)
, ("remove", remove)
, ("bump", bump)
]
main = do
(command:args) <- getArgs
let result = lookup command dispatch
if result == Nothing then
errorExit
else do
let (Just action) = result
action args
errorExit :: IO ()
errorExit = do
putStrLn "Incorrect command"
add :: [String] -> IO ()
add [fileName, todoItem] = appendFile fileName (todoItem ++ "\n")
view :: [String] -> IO ()
view [fileName] = do
contents <- readFile fileName
let todoTasks = lines contents
numberedTasks = zipWith (\n line -> show n ++ " - " ++ line) [0..] todoTasks
putStr $ unlines numberedTasks
remove :: [String] -> IO ()
remove [fileName, numberString] = do
handle <- openFile fileName ReadMode
(tempName, tempHandle) <- openTempFile "." "temp"
contents <- hGetContents handle
let number = read numberString
todoTasks = lines contents
newTodoItems = delete (todoTasks !! number) todoTasks
hPutStr tempHandle $ unlines newTodoItems
hClose handle
hClose tempHandle
removeFile fileName
renameFile tempName fileName
bump :: [String] -> IO ()
bump [fileName, numberString] = do
handle <- openFile fileName ReadMode
(tempName, tempHandle) <- openTempFile "." "temp"
contents <- hGetContents handle
let number = read numberString
todoTasks = lines contents
bumpedItem = todoTasks !! number
newTodoItems = [bumpedItem] ++ delete bumpedItem todoTasks
hPutStr tempHandle $ unlines newTodoItems
hClose handle
hClose tempHandle
removeFile fileName
renameFile tempName fileName
Trying to compile it gives me the following error:
$ ghc --make todo
[1 of 1] Compiling Main ( todo.hs, todo.o )
todo.hs:16:15:
No instance for (Eq ([[Char]] -> IO ()))
arising from a use of `=='
Possible fix:
add an instance declaration for (Eq ([[Char]] -> IO ()))
In the expression: result == Nothing
In a stmt of a 'do' block:
if result == Nothing then
errorExit
else
do { let (Just action) = ...;
action args }
In the expression:
do { (command : args) <- getArgs;
let result = lookup command dispatch;
if result == Nothing then
errorExit
else
do { let ...;
.... } }
I don't get why is that since lookup returns Maybe a, which I'm surely can compare to Nothing.
The type of the (==) operator is Eq a => a -> a -> Bool. What this means is that you can only compare objects for equality if they're of a type which is an instance of Eq. And functions aren't comparable for equality: how would you write (==) :: (a -> b) -> (a -> b) -> Bool? There's no way to do it.1 And while clearly Nothing == Nothing and Just x /= Nothing, it's the case that Just x == Just y if and only if x == y; thus, there's no way to write (==) for Maybe a unless you can write (==) for a.
There best solution here is to use pattern matching. In general, I don't find myself using that many if statements in my Haskell code. You can instead write:
main = do (command:args) <- getArgs
case lookup command dispatch of
Just action -> action args
Nothing -> errorExit
This is better code for a couple of reasons. First, it's shorter, which is always nice. Second, while you simply can't use (==) here, suppose that dispatch instead held lists. The case statement remains just as efficient (constant time), but comparing Just x and Just y becomes very expensive. Second, you don't have to rebind result with let (Just action) = result; this makes the code shorter and doesn't introduce a potential pattern-match failure (which is bad, although you do know it can't fail here).
1:: In fact, it's impossible to write (==) while preserving referential transparency. In Haskell, f = (\x -> x + x) :: Integer -> Integer and g = (* 2) :: Integer -> Integer ought to be considered equal because f x = g x for all x :: Integer; however, proving that two functions are equal in this way is in general undecidable (since it requires enumerating an infinite number of inputs). And you can't just say that \x -> x + x only equals syntactically identical functions, because then you could distinguish f and g even though they do the same thing.
The Maybe a type has an Eq instance only if a has one - that's why you get No instance for (Eq ([[Char]] -> IO ())) (a function can't be compared to another function).
Maybe the maybe function is what you're looking for. I can't test this at the moment, but it should be something like this:
maybe errorExit (\action -> action args) result
That is, if result is Nothing, return errorExit, but if result is Just action, apply the lambda function on action.

Read n lines into a [String]

I'm trying to read n lines of content into a List of Strings. I've tried several variations of the code below, but nothing worked.
main = do
input <- getLine
inputs <- mapM getLine [1..read input]
print $ length input
This throws the following error:
Couldn't match expected type `a0 -> IO b0'
with actual type `IO String'
In the first argument of `mapM', namely `getLine'
In a stmt of a 'do' block: inputs <- mapM getLine [1 .. read input]
In the expression:
do { input <- getLine;
inputs <- mapM getLine [1 .. read input];
print $ length input }
And
main = do
input <- getLine
let inputs = map getLine [1..read input]
print $ length input
throws
Couldn't match expected type `a0 -> b0'
with actual type `IO String'
In the first argument of `map', namely `getLine'
In the expression: map getLine [1 .. read input]
In an equation for `inputs': inputs = map getLine [1 .. read input]
How can I do this?
Use replicateM from Control.Monad:
main = do
input <- getLine
inputs <- replicateM (read input) getLine
print $ length inputs
In the spirit of give a man a fish / teach a man to fish: You could have found this yourself by searching Hoogle.
You have:
an action to perform of type IO String
a number of times to perform that action (type Int)
You want:
an action of type IO [String]
So you could search Hoogle for (IO String) -> Int -> (IO [String]). replicateM is the first hit.
One other way you can do this action is by using the pure replicate and the sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)
tool. Lets say first we will ask for a count and then ask for that many integers to print their sum on the terminal.
sumCountManyData :: IO ()
sumCountManyData = putStr "How many integers to sum..? "
>> getLine
>>= sequence . flip replicate getLine . read
>>= print . sum . map read

Resources