[Haskell-beginners] ignature for a list of a set length

Jonas Almström Duregård jonas.duregard at chalmers.se
Tue Sep 28 11:05:57 EDT 2010


A slightly more readable (IMO) version (which won't fail to terminate
on infinite lists):

getTernaryRelationship (ls@[a,b,c]) = ...
getTernaryRelationship _ = error "getTernaryRelationship: not a ternary rel"

You can of course have type (a,a,a) -> (a,a,a) unless your other
functions require lists

/J

On 28 September 2010 16:07, Martin Tomko <martin.tomko at geo.uzh.ch> wrote:
> Dear all,
> I want to have a function that acts only on lists of length 3 (I have a
> function that filters a list of lists and returns only those of that
> length). I guess I could change them into tuples (is there a way?), but
> anyway.
> Is there a way to specify in the signature that the function should only
> match such lists, or do I have to do pattern mattching in order to exclude
> other possibilities?:
>
> getTernaryRelationship :: [a] -> [a]
> getTernaryRelationship ls = if (length ls /= 3) then error "not a ternary
> rel" else ...
>
> thanks
> Martin
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list