V
V
Vitaly the Wise2017-10-15 21:05:34
Haskell
Vitaly the Wise, 2017-10-15 21:05:34

How to make beautiful inference in Haskell?

Hello. I'm doing a Haskell assignment. Here is the code

{-# OPTIONS_GHC -Wall #-}
module Main where
import System.IO()
import System.Environment

main :: IO ()
main = do args <- getArgs 
          if (length args > 0) then do
            f <- readFile $ head args
            putStrLn $ seqWord f
          else do
            f <- getContents
            putStrLn $ seqWord f

seqWord :: String -> String
seqWord [] = []
seqWord s = show $ unlines (map (unwords . filterWord . words) (lines s))

filterWord :: [String] -> [String]
filterWord [] = []
filterWord (x:xs) = x : filterWord (filter(/=x) xs)

The program takes as input either a file name and reads it or the text stdin. The essence of the program is to remove already encountered words in a line. The program works, but there are problems with the output. If output like this:
seqWord s = show $ unlines (map (unwords . filterWord . words) (lines s))

then you see something like this
"\"111111 1 11\"\n\"5555 5 55\"\n"
If you output it like this
seqWord s = unlines (map (unwords . filterWord . words) (lines s))

then in cases with a file, the result will be like this
1 12 5 8 13 145 85
546 822 1 12 58 8 9
444 4 44

But if you enter manually through the console, then when you press Enter, the result will be displayed immediately, but it should be displayed after Ctrl C.
Maybe someone knows how to fix this problem. Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wiz, 2020-09-01
@wiz

Show does not output, it only converts one value to another. PutStrLn and comrades are involved in the output. They require [Char] or String as input. If you already have a [Char] value, then you don't need to `show` it.
If you have a list of strings on hand, then you need to look at what the actual task is. If the lines need to be displayed one after another, without hyphens, then they can be glued together using `unwords`. The resulting string can be immediately shipped to `putStrLn`. If you need to display several lines one after another, then you can either glue them together through `unlines` and output them in one `putStrLn`, or call putStrln on each line from the list separately.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question