Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I am trying to write a something like a repl in Haskell and I want to replicate this code in C:
for (int c = getc(stdin); c != 'e'; c = getc(stdin)) {
printf("I got %c!\n", c);
}
I could use recursion but I am afraid of exeeding the limit.
You should use recursion. Haskell is designed to handle recursion without limits, and recursion can and should be used even for effectively infinite loops (e.g., event processing loops or REPL loops).
So, you could more or less write your program as follows:
main = do
txt <- getLine
if txt /= "exit" then do
putStrLn $ "I got " ++ txt ++ "!"
main
else do
return ()
giving:
$ ./repl
foo
I got foo!
bar
I got bar!
exit
$
I've written it to grab a whole line of input instead of single character, since there are typically issues with buffered input when trying to grab input character by character. This infinite recursion works fine and will not exceed any limit no matter how many billions of lines it processes before exiting.
In most real-world programs, you don't want the whole main program to loop, so you typically write something like the following. Here, I've used when which is a nicer looking way of writing the pattern if xxx then yyy else return ().
import Control.Monad -- for definition of "when"
main = do
-- initialization
putStrLn $ "Welcome to my REPL!"
-- start the main loop
loop
-- clean up
putStrLn $ "Thanks so much for using my REPL!"
-- definition of main loop
loop = do
txt <- getLine
when (txt /= "exit") $ do
putStrLn $ "I got " ++ txt ++ "!"
loop
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 days ago.
Improve this question
When creating a command-line app, one usually has to do some kind of parsing of command-line arguments, and print an error message if a different number of arguments is expected, or they do not make sense. For the sake of simplicity let's say that a program takes a positive integer as its only argument. Parsing and further program execution in Haskell can be done like this:
main :: IO ()
main = do
args <- getArgs
case args of
[arg] -> case readMaybe arg :: Maybe Int of
Just n | n > 0 -> runProg n
Just n -> die $ "expected a positive integer (got: " <> show n <> ")"
Nothing -> die $ "expected an integer (got: " <> arg <> ")"
_ -> die $ "expected exactly one argument (got: " <> show (length args) <> ")"
Creation of appropriate error message feels clunky to me, especially combined with show anywhere I want to include a non-string argument. There is printf but this on the other hand feels... not Haskell-y. What would be the idiomatic approach here? Perhaps my bias against the methods I listed is unjustified and it is, in fact, idiomatic Haskell?
As per the comment, if you're actually parsing command line arguments, you probably want to use optparse-applicative (or maybe optparse).
More generally, I think a reasonably idiomatic way of constructing complex error messages in Haskell is to represent the errors with an algebraic data type:
data OptError
= BadArgCount Int Int -- expected, actual
| NotInteger String
| NotPositive Int
supply a pretty-printer:
errorMessage :: OptError -> String
errorMessage (BadArgCount exp act) = "expected " <> show exp
<> " arguments, got " <> show act
errorMessage (NotInteger str) = "expected integer, got " <> show str
errorMessage (NotPositive n) = "expected positive integer, got " <> show n
and perform the processing in a monad that supports throwing errors:
data Args = Args Int
processArgs :: [String] -> Either OptError Args
processArgs [x] = case readMaybe x of
Just n | n > 0 -> pure $ Args n
| otherwise -> throwError $ NotPositive n
Nothing -> throwError $ NotInteger x
processArgs xs = throwError $ BadArgCount 1 (length xs)
This is certainly overkill for argument processing in a small command-line utility, but it works well in other contexts that demand complex error reporting, and it has several advantages over the die ... approach:
All the error messages are tabulated in one place, so you know exactly what errors the processArgs function can throw.
Error construction is type checked, reducing the potential for errors in your error handling code.
Error reporting is separated from error rendering. This is useful for internationalization, separate error reporting styles for terminal and non-terminal output, reuse of the functions in driver code that wants to handle errors itself, etc. It's also more ergonomic for development, since you don't have to take a break from "real coding" to make up a sensible error message. This typically results in better error reporting in the final product, since it encourages you to write a clear, consistent set of error messages all at once, after the core logic is finished.
It facilitates refactoring the errors systematically, for example to add location information (not relevant for command line arguments, but relevant for errors in input files, for example), or to add hints/recommendations for correction.
It's relatively easy to define a custom monad that also supports warnings and "non-fatal" errors that allow further error checking to continue, generating a list of errors all at once, instead of failing after the first error.
I haven't used this approach for command line arguments, since I usually use optparse-applicative. But, I have used it when coding up interpreters.
I am writing a program in Haskell which repeatedly takes its most recent result and uses this to compute the next result. I want to be able to see the newest result in response to user input, so I tried something like this:
main = mainhelper 0
mainhelper count = do
count <- return (count + 1)
line <- getLine
if null line
then do mainhelper count
else do
putStrLn $ show count
return ()
I was hoping that getLine would return an empty line if the user hasn't entered anything, but this doesn't happen, instead the program does nothing until it receives user input. Is there a way around this?
One simple solution is to fork a thread for the complicated computation and communicate with the main UI thread via MVar. For example:
import Control.Exception
import Control.Monad
import Control.Concurrent
thinkReallyHard x = do
threadDelay 1000000 -- as a proxy for something that's actually difficult
evaluate (x+1)
main = do
v <- newMVar 0
forkIO (forever (modifyMVar_ v thinkReallyHard))
forever (getLine >> readMVar v >>= print)
You may wonder about the role of evaluate in thinkReallyHard. The subtlety there is that MVars are lazy -- they can contain thunks just as easily as computed values. In particular, this means it's easy to accidentally push all the pure computation from the forked thread into the thread that's reading and using the contents of the MVar. The call to evaluate simply forces the forked thread to finish the pure computation before writing to the MVar.
It does return an empty line if you hit enter without entering text -- you just immediately prompt for more input, so it might look like nothing is happening. But if you run the program, hit enter three times, then enter something non-empty, you'll see that the final count reflects the multiple entries.
Here's a modified version of your code that does the same thing, but is slightly more canonical:
main = mainhelper 0
mainhelper count = do
let count' = count + 1
line <- getLine
if null line
then mainhelper count'
else print count'
Rather than count <- return (count + 1), you can write let count' = count + 1 -- this is a pure binding, not something that needs to invoke the IO monad (as you're doing with <- and return). But I used count' instead of count because otherwise that will create a recursive binding. The '-suffixing is a standard idiom for a "modified version" of an identifier.
Next I switched putStrLn . show to print, which is part of the Prelude and does exactly that.
I got rid of the return () because print (and putStrLn) already have the type IO (). This allows you to elide the do statements, as there's now a single IO expression in each branch of the if.
It's not really clear what you're trying to do here that's different from what you are doing -- the code does (in imperative terms) increment a counter every time the user presses enter, and displays the state of the counter every time the user enters some non-empty text.
Here's another version that prints the counter every time, but only increments it when prompted, which may or may not be helpful to you:
main = mainhelper 0
mainhelper count = do
print count
line <- getLine
mainhelper (if null line then count else succ count)
I'm using succ, the successor function, instead of the explicit + 1, which is just a style preference.
I'm learning some Haskell and I came across this small program
reverseLines :: String -> String
reverseLines input =
unlines (map reverse (lines input))
main :: IO ()
main = interact reverseLines
This program will keep asking the user for more input and reverse the input and print it on the screen.
Most of this is straight forward but one thing I can't wrap my head around is why does this function keeps running and ask the user for more input whereas if I just replace the reverseLines function with a function the simply returns some string it will not happen.
This program will stop after one execution:
foo input = "Stops"
main :: IO ()
main = interact foo
Why?
If you look at the source of interact you see this:
interact f = do s <- getContents
putStr (f s)
see the getContents? This is where the magic starts - it will read everything till EOF
Now in Haskell this is lazy-IO which can be bad but here is almost magical - see the string is read lazily and passed to your reverseLines - this one of course will only generate output as soon as it saw \n characters (the lines) and so it seems your program is some kind of REPL.
In the second one you don't consume any of the lazy-string at all so it stops ASAP
As I wrote in the comments you can play with this by either passing content into the program using a file (or echo) and pipes on the terminal:
echo "Hello World\nBye Bye" | runhaskell LazyIO.hs
or using CTRL-D to pass in the EOF yourself.
To get a feeling for it I would play with the functions more - what happens if you use something that needs to see the complete input first (try reverse without the maps)? What happens with words instead of lines, ...?
Have fun!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm fairly new to Haskell. I've been trying to create a utility to count the number of words and lines in Haskell for a few days to help me understand the language better. However, I'm struggling to get it working.
So far, I have:
wordCountUtility = do
putStrLn "Please enter the filename:"
filename <- getLine
putStrLn ("The file name you have entered is: " ++ filename)
contents <- readFile filename -- read the file specified in “name” into “contents”
lower <- (return . map toLower) contents
putStrLn lower
I have tried to use 'chop' and found print . length . words =<< getContents and have modified it a number of times, but I have had no luck.
I've also had a look at quite a few similar answers on Stack Overflow, such as : identifying number of words in a paragraph using haskell
The output should be somewhat similar to this:
Amount of Lines within the file
Lines : 10
Amount of Words found within the file
Words : 110
Any help would be much appreciated.
Your wordCountUtility should probably be not be counting words yet. You should just stop at something like
commandLineUtility :: (String -> String) -> IO ()
commandLineUtility fn = do
putStrLn "Please enter the filename:"
filename <- getLine
putStrLn ("The file name you have entered is: " ++ filename)
contents <- readFile filename -- read the file specified in “name” into “contents”
lower <- (return . fn) contents
putStrLn lower
(I am keeping this as close to your text as possible.) Now, though, you have to figure out what
(String -> String) function you want to apply. This is a pure function and should be developed separately. So you might write:
cwlcount :: String -> (Int, Int, Int)
cwlcount str = (length str, length (words str), length (lines str))
format :: (Int, Int, Int) -> String
format (c,w,l) = unlines $
["Number of characters:"
, show c
, "Number of words:"
, show w
, "Number of lines:"
, show l
]
So (format . cwlcount) :: String -> String and you can write:
main :: IO ()
main = commandLineUtility (format . cwlcount)
Of course there are a million objections to this program, but you can improve it by investigating the parts piecemeal. For one thing, it is irritating that the whole list of characters is brought into memory and three length calculations are made for it separately. The Predude.getLine is not very user friendly either...
At the moment, then, our results look so:
$ ghci Sonia_CS.hs
GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main ( Sonia_CS.hs, interpreted )
Ok, modules loaded: Main.
>>> main
Please enter the filename:
Sonia_CS.hs
The file name you have entered is: Sonia_CS.hs
Number of characters:
816
Number of words:
110
Number of lines:
25
Or better:
$ ghc -O2 Sonia_CS.hs
[1 of 1] Compiling Main ( Sonia_CS.hs, Sonia_CS.o )
Linking Sonia_CS ...
$ ./Sonia_CS
Please enter the filename:
/usr/share/dict/words
The file name you have entered is: /usr/share/dict/words
Number of characters:
2493109
Number of words:
235886
Number of lines:
235886
I'm trying to learn Haskell to get used to functional programming languages. I've decided to try a few problems at interviewstreet to start out. I'm having trouble reading from stdin and doing io in general with haskell's lazy io.
Most of the problems have data coming from stdin in the following form:
n
data line 1
data line 2
data line 3
...
data line n
where n is the number of following lines coming from stdin and the next lines are the data.
How do I run my program on each of the n lines one at a time and return the solution to stdout?
I know the stdin input won't be very large but I'm asking about evaluating each line one at a time pretending the input is larger than what can fit in memory just to learn how to use haskell.
You can use interact, in conjunction with lines to process data from stdin one line at a time. Here's an example program that uses interact to access stdin, lines to split the data on each newline, a list comprehension to apply the function perLine to each line of the input, and unlines to put the output from perLine back together again.
main = interact processInput
processInput input = unlines [perLine line | line <- lines input]
perLine line = reverse line -- do whatever you want to 'line' here!
You don't need to worry about the size of the data you're getting over stdin; Haskell's laziness ensures that you only keep the parts you're actually working on in memory at any time.
EDIT: if you still want to work on only the first n lines, you can use the take function in the above example, like this:
processInput input = unlines [perLine line | line <- take 10 (lines input)]
This will terminate the program after the first ten lines have been read and processed.
You can also use a simple recursion:
getMultipleLines :: Int -> IO [String]
getMultipleLines n
| n <= 0 = return []
| otherwise = do
x <- getLine
xs <- getMultipleLines (n-1)
return (x:xs)
And then use it in your main:
main :: IO ()
main = do
line <- getLine
let numLines = read line :: Int
inputs <- getMultipleLines numLines