Reading a continuous input terminating at a condition - haskell

I am totally new to Haskell. I am writing code to accept a series of values.
eg.
1 2
3 4
0 0
The last condition in when the input stops and I should display the values 1 2 3 4.
I have done the following, but it doesn't work. I need some help.
main = myLoop
myLoop = do inp <- getLine
if (inp == "0 0") then
putStrLn "END"
else do putStrLn(inp)
myLoop

First of all, make sure you are not using tabs in your source. In order to get your example to work I had to line up the putStrLn and the myLoop like this:
myLoop = do inp <- getLine
if (inp == "0 0") then
putStrLn "END"
else do putStrLn(inp)
myLoop
-- ^ note putStrLn and myLoop are at the same column
Secondly, I'll answer the question assuming you want to read in a list of numbers / words.
readNums :: IO [String]
readNums = do
x <- getLine
if x == "0 0"
then return []
else do xs <- readNums
return (words x ++ xs)
Example usage:
main = do nums <- readNums
print nums

Related

End program after number of inputs

What my program does? Take input from user, it needs to be between 0-99.
Program ends when user guesses the number "guess" which is 45 or after taking 10 tries.
The problem is that I cant figure out the second part of this game.
guess = 45
game = do
putStrLn ("Give a number between 0 and 99")
a <- getLine
let x = read a
if x == guess then print ("You got it!") else game
You can make a helper function where you use a parameter with the number of guesses. Each time you make a recursive call where you decrement the number of guesses, if the variable is less than or equal to zero, we can stop the recursion:
guess :: Int
guess = 45
tries :: Int
tries = 10
main :: IO ()
main = game tries
game :: Int -> IO ()
game n
| n <= 0 = putStrLn "end of the game"
| otherwise = do
putStrLn "Give a number between 0 and 99"
a <- readLn
if a == guess then putStrLn "You got it!" else game (n-1)
Thank you Willem! Had to rearrange some things in the code but you gave me the idea. Thats how now it looks like:
guess = 45
tries = 10
main = zad5 tries
zad5 y = do
putStrLn ("Give a number between 0 and 99")
a <- getLine
let x = read a
if y==1 || x == guess then print ("end of the game") else zad5 (y-1)

How to read/ add/ print a number string in Haskell

Below if my code right now. I want to be able to take in user input like the following: "6 1 2 3 4 5 6" and the get the sum and print. it would also be cool to understand how to use the first number entered as the total numbers. SO here the first number is 6 and the total numbers inputted is 6.
Thank you in advance for helping me with this. I have been researching for weeks and cannot figure this out.
main = do
putStrLn "Enter how many numbers:" -- clearer
num<-getLine
putStrLn("Enter a number: ")
numberString <- getLine
let numberInt =(read numberString :: Int)
print (numberInt*4)
main
It seems you either need an auxiliary recursive function for reading num integers, or some helper like replicateM, which makes writing the code a little easier.
replicateM num action runs action exactly num times, and collects all the action results in a list.
main = do
putStrLn "Enter how many numbers:" -- clearer
num<-getLine
numbers <- replicateM num $ do
putStrLn("Enter a number: ")
numberString <- getLine
return (read numberString :: Int)
-- here we have numbers :: [Int]
...
You can then continue from there.
If instead you want to use an auxiliary function, you can write
readInts :: Int -> IO [Int]
readInts 0 = return []
readInts n = do
putStrLn("Enter a number: ")
numberString <- getLine
otherNumbers <- readInts (n-1) -- read the rest
return (read numberString : otherNumbers)
Finally, instead of using getLine and then read, we could directly use readLn which combines both.
Construct a list of integers using
let l = map (\x -> read x::Int) (words "6 1 2 3 4 5 6")
in (numNumbers, numbers)
You tried to read the whole string into a single number.

haskell parse error on input `=' Perhaps you need a 'let' in a 'do' block? e.g. 'let x = 5' instead of 'x = 5'

Can't get my haskell program to work
sort [] = []
sort (x:xs) = sort [a | a <- xs , a<=x ] ++ [x] ++ sort [a | a <- xs , a > x]
getList:: Int->[IO Int]
getList 0 = [] --declaring the empty list
getList n = [a | a <- [getNumber] ] ++ getList (n-1)
getNumber::IO Int --get number function
getNumber = do
s <- getLine
return (read s)
--Main function to handle
main = do
p <- getNumber -- taking the number of variable
lst <- sequence (getList p) --calling gtlistFunction to input the list
print (sort lst) --print
Error:
quicksort.hs:13:6: error:
parse error on input `='
Perhaps you need a 'let' in a 'do' block?
e.g. 'let x = 5' instead of 'x = 5'
|
13 | main = do
| ^
You must indent all do-blocks:
-- NOT correct: this will fail
main = do
putStrLn "This is..."
putStrLn "WRONG!"
-- Correct:
main = do
putStrLn "This is..."
putStrLn "correct!"
The number of spaces doesn't matter as long it is consistent.
To avoid this you could use c-like notation:
-- Also correct:
main = do {
putStrLn "This is...";
putStrLn "also correct!";
}
And this leaves you free to indent as you like. Example:
-- Also correct:
main = do
{ putStrLn "This is..."
; putStrLn "also correct!"
;}
Without indentation, the parser doesn't know that main = ... starts a new definition, separate from the preceding definition of getNumber. The following would work fine:
getNumber::IO Int
getNumber = do
s <- getLine
return (read s)
main = do
p <- getNumber
lst <- sequence (getList p)
print (sort lst)
The transition from one-space indentation to no indentation is enough to tell the parser that return (read s) is the last line of the do block defining getNumber, and that main = do is the beginning of a new top-level definition.
Either indenting your code, or using the explicit brace syntax in AJFarmar's answer, lets the parser know where one definition ends and the next begins.

Haskell: Print case number

I have written a Haskell code as:
loop = do
x <- getLine
if x == "0"
then return ()
else do arr <- replicateM (read x :: Int) getLine
let blocks = map (read :: String -> Int) $ words $ unwords arr
putStr "Case X : output = "; -- <- What should X be?
print $ solve $ blockPair blocks;
loop
main = loop
This terminates at 0 input. I also want to print the case number eg. Case 1, 2 ...
Sample run:
1
10 20 30
Case 1: Output = ...
1
6 8 10
Case 2: Output = ...
0
Does anyone know how this can be done? Also, If possible can you suggest me a way to print the output line at the very end?
Thanks in advance.
For the first part of your question, the current case number is an example of some "state" that you want to maintain during the course of your program's execution. In other languages, you'd use a mutable variable, no doubt.
In Haskell, there are several ways to deal with state. One of the simplest (though it is sometimes a little ugly) is to pass the state explicitly as a function parameter, and this will work pretty well given the way you've already structured your code:
main = loop 1
loop n = do
...
putStr ("Case " ++ show n ++ ": Output = ...")
...
loop (n+1) -- update "state" for next loop
The second part of your question is a little more involved. It looks like you wanted a hint instead of a solution. To get you started, let me show you an example of a function that reads lines until the user enters end and then returns the list of all the lines up to but not including end (together with a main function that does something interesting with the lines using mostly pure code):
readToEnd :: IO [String]
readToEnd = do
line <- getLine
if line == "end"
then return []
else do
rest <- readToEnd
return (line:rest)
main = do
lines <- readToEnd
-- now "pure" code makes complex manipulations easy:
putStr $ unlines $
zipWith (\n line -> "Case " ++ show n ++ ": " ++ line)
[1..] lines
Edit: I guess you wanted a more direct answer instead of a hint, so the way you would adapt the above approach to reading a list of blocks would be to write something like:
readBlocks :: IO [[Int]]
readBlocks = do
n <- read <$> getLine
if n == 0 then return [] else do
arr <- replicateM n getLine
let block = map read $ words $ unwords arr
blocks <- readBlocks
return (block:blocks)
and then main would look like this:
main = do
blocks <- readBlocks
putStr $ unlines $
zipWith (\n line -> "Case " ++ show n ++ ": " ++ line)
[1..] (map (show . solve . blockPair) blocks)
This is similar in spirit to K. A. Buhr's answer (the crucial move is still passing state as a parameter), but factored differently to demonstrate a neat trick. Since IO actions are just normal Haskell values, you can use the loop to build the action which will print the output without executing it:
loop :: (Int, IO ()) -> IO ()
loop (nCase, prnAccum) = do
x <- getLine
if x == "0"
then prnAccum
else do inpLines <- replicateM (read x) getLine
let blocks = map read $ words $ unwords inpLines
prnAccumAndNext = do
prnAccum
putStr $ "Case " ++ show nCase ++ " : output = "
print $ solve $ blockPair blocks
loop (nCase + 1, prnAccumAndNext)
main = loop (1, return ())
Some remarks on the solution above:
prnAccum, the action which prints the results, is threaded through the recursive loop calls just like nCase (I packaged them both in a pair as a matter of style, but it would have worked just as fine if they were passed as separate arguments).
Note how the updated action, prnAccumAndNext, is not directly in the main do block; it is defined in a let block instead. That explains why it is not executed on each iteration, but only at the end of the loop, when the final prnAccum is executed.
As luqui suggests, I have removed the type annotations you used with read. The one at the replicateM call is certainly not necessary, and the other one isn't as well as long as blockPair takes a list of Int as an argument, as it seems to be the case.
Nitpicking: I removed the semicolons, as they are not necessary. Also, if arr refers to "array" it isn't a very appropriate name (as it is a list, and not an array), so I took the liberty to change it into something more descriptive. (You can find some other ideas for useful tricks and style adjustments in K. A. Buhr's answer.)

Haskell: How to print multiple metrics of pure function results

I want to print out the average and length of a list of values resulting from a "pure" function call. I've done a fair bit of research on this and just about to put my head through a wall. The following code works:
[... a bunch of other stuff like "doParallelTrades" ...]
simulate :: Int -> Int -> [Double]
simulate numbuyers groupsize =
let buyers = initTraders numbuyers minimum_price maximum_price
sellers = initTraders numbuyers minimum_price maximum_price
in doParallelTrades sellers buyers groupsize
{--
MAIN
--}
getNum :: IO Int
getNum = readLn
main :: IO ()
main = do
let prices n = simulate n 0
putStr "How many buyers?"
n <- getNum
start <- getCurrentTime
print "average price = "
print $ average $ prices n
end <- getCurrentTime
print "number of trades:"
print $ length $ prices n
print "Wall Time:"
print (diffUTCTime end start)
However, this evaluates "prices n" twice, which obviously I don't want to do for large n. I'd like to evaluate it just once, then compute and print the average, then print the length. I tried changing the main function to:
main = do
let prices n = simulate n 0
putStr "How many buyers?"
n <- getNum
start <- getCurrentTime
p <- prices n -- ***********New Code*********
print "average price = "
print $ average $ p -- ***********New Code**********
end <- getCurrentTime
print "number of trades:"
print $ length $ p -- **********New Code***********
print "Wall Time:"
print (diffUTCTime end start)
I.e., bind "prices n" to "p" and then do stuff with p. But the interpreter gives me the error
zitraders-threaded.hs:162:8:
Couldn't match expected type β€˜IO [Integer]’
with actual type β€˜[Double]’
In a stmt of a 'do' block: p <- prices n
I've researched various online resources but they are either too simple (working only within the IO monad) or too complex. So how do I:
Get some input from the user.
Run some "pure" calculations on that input (just once) and store the result.
Tell the user multiple properties of that result.
You can not bind p with prices n with the '<-' operator, use a let instead:
This should do the trick:
main = do
...
start <- getCurrentTime
let p = prices n -- ***********New Code*********
print "average price = "
...

Resources