So I've been enjoying this challenging language, I am currently working on an assignment for school.
This is what it says: I need to prompt the user for a list of numbers, then display the average of the list , I am so close to figuring it out. However I get this weird parse error:
"Exception: user error (Prelude.readIO: no parse)"
Here is my code:
module Main (listM', diginums', getList, main) where
import System.IO
import Data.List
diginums' = []
listM' = [1, 2, 3]
average' = (sum diginums') / (fromIntegral (length diginums'))
getList :: IO [Double]
getList = readLn
main = do
putStrLn "Please enter a few numbers"
diginums' <- getList
putStrLn $ show average'
Terminal Prompts : Enter a few #'s
I Enter : 123
ERROR : Exception: user error (Prelude.readIO: no parse)
I know my functions are working correctly to calculate the average. Now I think my problem is that when I take in the list of numbers from the user, I don't correctly parse them to type Double for my average function.
Your type signature says that
getList :: IO [Double]
getList = readLn
reads a list of Doubles, that means it expects input of the form
[123, 456.789, 1011.12e13]
but you gave it what could be read as a single number, "123". Thus the read fails, the input couldn't be parsed as a [Double].
If you want to parse input in a different form, not as syntactically correct Haskell values, for example as a space-separated list of numbers, you can't use readLn, but have to write a parser for the desired format yourself. For the mentioned space-separated list of numbers, that is very easy, e.g
getList :: IO [Double]
getList = do
input <- getLine
let nums = words input
return $ map read nums
If you want to get the list in the form of numbers each on its own line, ended by an empty line, you'd use a recursion with an accumulator,
getList :: IO [Double]
getList = completeList []
completeList :: [Double] -> IO [Double]
completeList acc = do
line <- getLine
if null line
then return (reverse acc)
else completeList (read line : acc)
Parsers for more complicated or less rigid formats would be harder to write.
When the parsing is fixed, you run into the problem that you haven't yet got used to the fact that values are immutable.
diginums' = []
listM' = [1, 2, 3]
average' = (sum diginums') / (fromIntegral (length diginums'))
defines three values, the Double value average' is defined in terms of the empty list diginums', hence its value is
sum diginums' / fromIntegral (length diginums') = 0 / 0
which is a NaN.
What you need is a function that computes the average of a list of Doubles, and apply that to the entered list in main.
average :: [Double] -> Double
average xs = sum xs / fromIntegral (length xs)
main = do
putStrLn "Please enter a few numbers"
numbers <- getList
print $ average numbers
There are no mutable variables in Haskell, but it looks like you are trying to initialise diginums' as an empty list and then populate it with getList.
Instead, maybe you want to pass the list of numbers to average' as an argument, something like:
module Main (getList, main) where
import System.IO
import Data.List
average' ds = (sum ds) / (fromIntegral (length ds))
getList :: IO [Double]
getList = readLn
main = do
putStrLn "Please enter a few numbers"
diginums' <- getList
putStrLn $ show $ average' diginums'
Also, as Daniel said, you need to input using Haskell literal List syntax, given the way you've coded it.
Related
I want to have a function that reads arbitrary int's until the number '0' is inserted, and then presents the numbers inserted in an ordered list.
For that i wrote this function:
import Data.List
readIntegers :: IO()
readIntegers = do
putStrLn "insert a number: "
num<-getLine
let list = ordList ((read num :: Int):list)
if (read num == 0)
then print list
else readIntegers
where ordList ::[Int]->[Int]
ordList [] = []
ordList xs = sort xs
This compiles just fine, but when i insert the number '0', it gives me this error:
*** Exception: <<loop>>
What am i doing wrong ?
As #phg points out, you are essentially constructing an infinite list, and actually evaluating it causes the loop error. A simple implementation to resolve this issue is to define a helper function which takes an additional parameter - a list to store all the inputs read in from the screen, like so:
readInteger :: IO ()
readInteger = readInteger' []
where
readInteger' x = do
putStrLn "insert a number: "
num<-getLine
if ((read num :: Int) == 0)
then print $ ordList x
else readInteger' $ (read num :: Int):x
where ordList ::[Int]->[Int]
ordList [] = []
ordList xs = sort xs
Please note that the above is essentially just an implementation of #phg's answer, but with some changes to your original logic. Firstly, since 0 is a sentinel value, we shouldn't be appending that to our list. Second, we do not need to sort the list every single time we are adding a value to it. Sorting once at the time of printing/passing to another function is sufficient.
Demo
If you want to read an unspecified number of integers without prompting for user input and cut it off the moment you encounter 0, you would probably do well to use getContents, which will read everything from the standard input as a single string, lazily.
Then, it is a simple matter of parsing it to a list of numbers and doing what you want with it, like so:
readIntegers :: ()
readIntegers = do
a <- getContents
let b = ordList $ takeWhile (/= 0) $ map (\x -> read x :: Int) $ words a
mapM (putStrLn . show) b
where ordList ::[Int]->[Int]
ordList [] = []
ordList xs = sort xs
let list = ordList ((read num :: Int):list)
This is basically a recursive definition of a list of the form [x, x, ...] (like if you wrote an equation saying x = 1 + x). That is perfectly fine by itself, since Haskell is lazy; however, if you try to print list (aka "solve the equation"), it will fail, since it will try to print infinitely many numbers.
You probably have a misconception about the workings of the (:) operator. Haskell functions will never perform an assignment operation and concatenate num onto list by changing it, like in imperative languages. There are only pure functions.
If you want to accumulate all numbers, you should try to come up with a recursive definition of readIntegers, keeping its state (the list) in an additional parameter (there are also more sophisticated ways, hiding the state passing, but more complicated to use for a beginner).
For a more sophisticated solution, note that this is an unfold and you can use unfoldM from Control.Monad.Loops to implement it:
import Control.Monad.Loops (unfoldM)
readInts :: IO [Int]
readInts = unfoldM $ fmap (check . read) getLine
where check x = if x == 0 then Nothing else Just x
This has the nice property that it returns the list in the order in which it was read.
guys can you help me fix this code?
import Data.Char
import Data.List
-- 1 2 3 4 5 6 7 8
colors = [1,2,3,4,5]
--game :: [Integer] -> [Char]
game a = do
let black = test a color
white = (test2 a color) - black
let x = [a] ++ createScore black white
show x
test [] [] = 0
test (x:xs) (y:ys) = if x == y then 1+test xs ys else 0+test xs ys
test2 a b = length (intersection a b)
intersection first second = [n | n <- first , isInfixOf [n] second]
createScore c b = [(take c (repeat 1) ++ take b (repeat 0))]
start = do
a <- getLine
let b = map read $ words a
--print b
game b
start
I have problem with IO and nonIO functions.
description of the program:
read data from line
convert data to List of int
call function game (which takes List of int like parametr)
make some calculation
print score
start again from 1
Problem is in function start and I have no idea how to fix it.
Thanks for help.
there are a few things that don't quite work with the code you've given us.
I'll work through the compile errors.
On first load we get a name error:
ex1.hs:10:26:
Not in scope: 'color'
Perhaps you meant 'colors' (line 6)
(and again at line 11)
Of course the compiler is correct and we just need to change the appropriate names to match.
Next we get the interesting one I assume you're referring to with regards to IO and non-IO functions:
ex1.hs:28:7:
Couldn't match type '[]' with 'IO'
Expected type: IO Char
Actual type: String
In the return type of a call of 'game'
In a stmt of a 'do' block: game b
In the expression:
do { a <- getLine;
let b = map read $ words a;
game b;
start }
The error is your use of game b in the IO block.
The type annotation you have commented out over the function definition of game is actually correct -- it is [Integer] -> [Char].
As such it's a pure function and we don't need to use the do notation to describe it like we would with something that deals with IO -- because you have used the notation here with an argument of a list, the do expression represents a computation in the context of a list, not an IO computation, so calling it from start has a type mismatch, it expects IO, but it has found [].
We can start fixing it up by turning game into a pure function, using a let-in expression.
game :: [Integer] -> [Char]
game a = let black = test a colors
white = (test2 a colors) - black
x = [a] ++ createScore black white
in show x
So now we have a function that returns the string of the input and it's score.
The compiler now gives the error Expected type: IO Char, Actual type: [Char], this is because we are still trying to use a non-IO expression in the main do block.
We can fix this by actually printing the string to stdout, just using print, so your original
--print b
game b
can just be
print $ game b
At this point the program compiles!
Unfortunately it's still not quite right, when we run this and type in a list of integers like 1 2 3 we get the exception ex1.hs:(14, 1)-(15,66): Non-exhaustive patterns in function test.
This one comes down to your definition of test as:
test [] [] = 0
test (x:xs) (y:ys)
Doesn't account for the possibility of one list being empty -- because the check is always between the head elements of the lists probably the smallest change to fix this could just be:
test (x:xs) (y:ys) = if x == y then 1+test xs ys else 0+test xs ys
test _ _ = 0
And now the program compiles and executes. Hopefully that makes sense.
game is (or should be) a pure function, so you need to call it with a function that returns an IO value in your start function, e. g. print $ game b.
There is also no reason to use do notation there (you can because [a] is also a monad, but you don't take advantage of it in any way). You can just use game a = show x and replace the let statements with a where block (or use let ... in).
test should be able to deal with the case of one list being empty and the other not, or you need to ensure that both lists are always the same size.
Im trying to write a simple function to learn the IO monad in Haskell. The function is supposed to take the sum of some given integers from the console but when the function has run for example 4 times it says "1*** Exception: Char.digitToInt: not a digit '\n'"
import Data.Char
readInts :: IO ()
readInts = do
putStrLn "Choose nr of ints to sum."
c <- getChar
let i = digitToInt c
-- code.
let sum = getInts i
let str = "\nSum is: " ++ [intToDigit i].
putStrLn str
getInt :: IO Int
getInt = do
c <- getChar
return (digitToInt c)
getInts :: Int -> IO Int
getInts n = if n == 0
then return 0
else
do i <- getInt
j <- getInts
return (i+j)
Can somebody please explain where my recursion is going wrong?
You're simply using the wrong tools to convert between "keyboard data" and numbers. Has little to do with IO.
intTodigit, as the name says, acts on single digits / characters, not on general numbers. What you want is to read / print entire strings, which can handle multi-digit numbers. Replace getChar with getLine, digitToInt with read, and [intToDigit i] with show i. Then it should work fine.
However, it would be better to make some more simplifications.
getInt basically exists already, though in more general form: readLn gets a line from stdin and inteprets it as the required type.
getInts is implemented more complicately than necessary. Explicit recursion over a counting variable (BTW, it has to be getInts (n-1) in the recursion) is ugly; such looping is obviously so common that there exists a standard solution (you need to import Control.Monad) for it which looks alot like loops in imperative languages you might be used to:
getIntsAndSum :: Int -> IO Int
getIntsAndSum n = fmap sum . forM [1 .. n] $ \k -> do
i <- getInt
return i
which can in fact be further simplified to
fmap sum . forM [1 .. n] $ \_ -> getInt
because do blocks a just an optional construct to help you chain actions, but when there's only one action you can simply write that on its own.
Ideally, you would not have to first ask for the number of numbers at all: just gather all numbers you're given, and sum them up. This works, as jamshidh said, very simply with interact.
main = interact processInput
where processInput allInput = show (sum allNumbers)
where allNumbers = map read . lines allInput
and that's it! Nothing else needed. In fact, this can be written yet simpler: you basically have just one simple data-flow pipeline.
main = interact $ show . sum . map read . lines
getChar gets every char, including any "\n" that you type in (which you need to submit the answer). Try filtering these out.
A better way to solve this could be to use "interact" to get the user input, and break apart the data using lines. That way the user could input multi digit numbers, and the "\n"'s would be removed.
The following sums numbers entered at the command line (without any prompting, end inputting by typing ^d).
main = interact (show . sum . fmap read . lines)
I'm trying to grab a random item from a string list and save that into another string list but I can't get my code to work.
import System.Random
import Control.Applicative ( (<$>) )
food = ["meatballs and potoes","veggisoup","lasagna","pasta bolognese","steak and fries","salad","roasted chicken"]
randomFood xs = do
if (length xs - 1 ) > 0 then
[list] <- (fmap (xs!!) $ randomRIO (0, length xs -1))
else
putStrLn (show([list])
I'm getting parse error on input '<-' but I'm sure there are more issues then that. There is also the issue that the list may contain the same dishes two days in a row which is not what I want and I guess I can remove duplicates but that also would remove the number of items in the list which I want to stay the same as the number in the list.
Anyone have a good idea how I could solve this? I have been searching for a day now and I can't find something useful for me but that's just because I'm looking in the wrong places. Any suggestion on how I can do this or where I can find the info will be greatly appreciated!
The reason it didn't work is that you needed another do after your if...then. (After a then you need an expression, not a pattern <- expression.)
randomFood :: String -> IO () -- type signature: take a String and do some IO.
randomFood xs = do
if length xs > 1 then do
[list] <- (fmap (xs!!) $ randomRIO (0, length xs -1))
else
putStrLn (show([list])
But that still doesn't compile, because you don't actually do anything with your list.
At the end of every do block, you need an expression to return.
I think you meant to still print some stuff if the length of xs is too short, and you probably meant to print the selected food if there was more than one to choose from.
Better would be:
randomFood :: String -> IO ()
randomFood xs | length xs <= 1 = putStrLn $ show xs
randomFood xs | otherwise = do
item <- (xs!!) <$> randomRIO (0, length xs -1)
putStrLn $ show(item)
This | boolean test = syntax is better for conditional answers based on input.
I changed [list] to item because you're selecting a single item randomly, not a list of items.
Haskell is quite happy to let you put [list], because any string that's got one character in it matches [list].
For example, "h" = [list] if list='h', because "h" is short for ['h']. Any longer string will give you Pattern match failure. In particular, all the food you've specified has more than one character, so with this definition randomFood would never work! item will match anything returned by your randomRIO expression, so that's fine.
You imported <$> then didn't use it, but it's a nice operator, so I've replaced fmap f iothing with f <$> iothing.
I finally realised I'm doing the wrong thing with short lists; if I do randomFood ["lump of cheese"] I'll get ["lump of cheese"], which is inconsistent with randomFood ["lump of cheese"] which will give me "lump of cheese".
I think we should separate the short list from the empty list, which enables us to do more pattern matching and less boolean stuff:
randomFood :: String -> IO ()
randomFood [] = putStrLn "--No food listed, sorry.--"
randomFood [oneitem] = putStrLn . show $ oneitem
randomFood xs = do
item <- (xs!!) <$> randomRIO (0, length xs -1)
putStrLn . show $ item
This gives three different definitions for randomFood depending on what the input looks like.
Here I've also replaced putStrLn (show (item)) with putStrLn . show $ item - compose the functions show and putStrLn and apply ($) that to the item.
Few points to note :
Don't intermix pure and impure code.
Try to use library for a task rather than repeating what is already written.
Here is the code using random-fu library
import Data.Random
import Control.Applicative
food :: [String]
food = ["meatballs and potoes","veggisoup","lasagna","pasta bolognese","steak and fries","salad","roasted chicken"]
randomFood :: [String] -> RVar (Maybe String)
randomFood [] = return Nothing
randomFood xs = Just <$> randomElement xs
main :: IO ()
main = (sample $ randomFood food) >>= print
This is like choosing one element from a list randomly.
> main
Just "steak and fries"
> main
Just "meatballs and potoes"
If you want to output just a random permutation of the above list, you can use shuffle like
main = (sample $ shuffle food) >>= print
Example
> main
["meatballs and potoes","lasagna","steak and fries","roasted chicken","salad","pasta bolognese","veggisoup"]
> main
["roasted chicken","veggisoup","pasta bolognese","lasagna","steak and fries","meatballs and potoes","salad"]
Basically what I need to do is write a function that takes in a list of type [(String, String)] and prints out the contents so that, line-by-line, the output looks like this:
FirstString : SecondString
FirstString : SecondString
..etc, for every item in the list. I've got the following code and it prints it out, but for some reason it prints out a line containing [(),()] at the end.
display :: Table -> IO ()
display zs = do {
xs <- sequence [putStrLn ( a ++ " = " ++ b) | (a, b) <- zs];
print xs
}
Is there anything I'm doing wrong?
The final print xs is unnecessary. sequence here is returning a bunch of ()s (the return value of putStrLn), and print is printing that out as well.
While you're at it, now that print xs is gone, you can get rid of the xs variable binding, and make sequence into sequence_ to throw away the return value, giving:
display :: Table -> IO()
display zs = sequence_ [putStrLn (a++" = "++b) | (a,b) <- zs]
You could even use mapM:
display :: Table -> IO ()
display = mapM_ (\(a,b) -> putStrLn (a++" = "++b))
I would agree with ja that you should split up your code in to two functions:
A pure part: a function that takes your data structure and turns it into a string
An impure part, that renders that string to the console
Here's a simple implementation:
showTable :: Table -> String
showTable xs = concatMap format xs
where
format (a, b) = a ++ " : " ++ b ++ "\n"
display :: Table -> IO ()
display table = putStr (showTable table)
This design has two advantages:
For one, most of your `logic' is in the pure part of the code, which is nice, in a functional programming kind of way.
Secondly, and this is just simple software engineering principle; you now have a reusable function that you can use, should you ever want to format your data structure in another part of your code (seems likely).
Write a function that takes a tuple to a string, formatted as you wish.
Then concatMap that function over your list; print the result.