Type checking of a self-defined function

haskell

Solution

The type `Ord a => [a] -> [b]` is internally consistent, though!

The problem is that you never actually add any elements from the input list to the output list. You need a base case; something like `foo [x] = [x]`. As it stands, you never actually say that any elements from the input list get added to the output list; the result of this function will always be `[]`, which can have type `[b]` regardless of input.

If you're trying to implement something like Quicksort here, though, there are two logical problems in your implementation:

- `x`, the pivot, doesn't get added to the output list.

- Any values in the list that are equal to `x` other than `x` itself will be added twice, once from `us` and once from `ys`.

Problem

I got this function: ``` foo [] = [] foo (x:xs) = foo us ++ foo ys where us = filter (<=x) xs ys = filter (>=x) xs ``` type of this function is Ord a => [a] -> [b] . I don't understand why the output type is [b] and not [a]. I think it should be [a] since the elements of the output list will be part of the elements of the input list. I am using Hugs, but I don't think it changes anything.

Original source