user66309
user66309

Reputation: 113

Haskell: input a list and output its minimum value

I am learning Haskell and I want to take as input a list, and output the minimum value using the function minimum.

    module Main where

    main = do putStrLn "Enter list"
              list <- readLn
              putStr "minimum list = "
              print (minimum list)

However I get some errors:

    No instance for (Show a0) arising from a use of `print'
    The type variable `a0' is ambiguous

and:

    No instance for (Ord a0) arising from a use of `minimum'
    The type variable `a0' is ambiguous

I have been reading up on Haskell typeclasses and type signatures from various sources around the web, but so far nothing has illuminated the course of action most amenable to the aforementioned quandary.

Upvotes: 1

Views: 316

Answers (1)

&#216;rjan Johansen
&#216;rjan Johansen

Reputation: 18199

This is because your type really is ambiguous: The code could work for any type that implements Ord and Show, and the compiler doesn't know which one to choose. To fix this, add a type annotation somewhere to tell it which type you want it to use. E.g. for a list of Integers:

module Main where

main = do putStrLn "Enter list"
          list <- readLn
          putStr "minimum list = "
          print (minimum list :: Integer)

Upvotes: 3

Related Questions