[Haskell-cafe] Newbie list question

David Tolpin david.tolpin at gmail.com
Sun May 27 05:17:18 EDT 2007


> type Person = (NI, Age, Balance)
> type Bank = [Person]
>
> credit :: Bank -> [Person]
> credit [(a,b,c)] = [(a,b,c)] if c >= 0
> 			then [(a,b,c)]
> 			else error "overdrawn customer"
>
> except this doesn't work with things like:
>
> credit [(1,2,3),(4,5,6)]
>

Hi,

that's because Haskell syntax is made for brains with high modality. When you declare a type, writing a type signature in square brackets make it to be a list of arbitrary number of elements of the inner type; when you write a pattern, one with an element in square brackets matches a single-element list. What you want is to

credit abcs = filter (\(a,b,c) -> c>=0) abcs

And if you think it looks like a machine-level assembly language, then you are probably right.

David


More information about the Haskell-Cafe mailing list