[Haskell-beginners] Reading input
cmb21 at kent.ac.uk
Wed Aug 20 16:34:23 EDT 2008
On another note, you may want to avoid monads altogether for this. You can
run this straight from ghci (replacing mainProg with main)
mainProg :: [Int] -> Int
mainProg arg = median lst
and then call mainProg with your desired list of stings
> mainProg [1,2,3,4]
On Wed, 20 Aug 2008, Yitzchak Gale wrote:
> Hi Barry,
> Barry Burd wrote:
> > median :: [Int] -> Int
> > median lst =
> > case medians lst of
> > -- All the values in the result are the same, we just pick the first one
> > x:xs -> x
> > main = do
> > putStr "Enter a list: "
> > lst <- getLine
> > median lst
> > I'm sure this is because Haskell isn't automatically changing a String
> > to a List of numbers. But how can I do this?
> Use the "read" function. Watch out, though - there is
> no error checking there. So if the user enters a string
> that does not have the right syntax for a Haskell list
> of integers, your program will halt with an error message.
> You have another problem - you used the "median"
> function as a step in a do block - but each step in a
> do block must have type "IO a" for some type a.
> Here's how you could write your main function:
> main = do
> putStr "Enter a list: "
> lst <- getLine
> print $ median $ read lst
> Beginners mailing list
> Beginners at haskell.org
More information about the Beginners