[Haskell-cafe] List as input

Janis Voigtlaender voigt at tcs.inf.tu-dresden.de
Wed Oct 15 02:51:44 EDT 2008


leledumbo wrote:
> module Main where
> 
> import Data.List
> 
> -- quicksort of any list
> qsort []     = []
> qsort (x:xs) = qsort(filter(<x) xs) ++ [x] ++ qsort(filter(>=x) xs)
> 
> -- optimized quicksort, uses middle element as pivot
> qsortOpt [] = []
> qsortOpt x  = qsortOpt less ++ [pivot] ++ qsortOpt greater
>   where
>     pivot = x !! ((length x) `div` 2)
>     less = filter (<pivot) (delete pivot x)
>     greater = filter (>=pivot) (delete pivot x)
> 
> main = do
>   putStr "Enter a list: "
>   l <- readLn
>   print (qsortOpt l)
> -- end of code
> 
> Why do I get
> 
> ERROR "qsort.hs":17 - Unresolved top-level overloading
> *** Binding             : main
> *** Outstanding context : (Read b, Show b, Ord b)

The compiler doesn't know what kind of list you are trying to read, 
sort, and print. Try something like:

(l::[Int]) <- readLn

in the penultimate line.

-- 
Dr. Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:voigt at tcs.inf.tu-dresden.de


More information about the Haskell-Cafe mailing list