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

David Virebayre dav.vire+haskell at gmail.com
Wed Sep 29 03:23:25 EDT 2010


2010/9/28 Martin Tomko <martin.tomko at geo.uzh.ch>:
> As responded earlier, I may try the maybes, but I am intrigued by the
> ls@[a,b,c]  and I cannto find any documentation about it. If you would not

When pattern matching a list of 3 elements,

- ls is the whole 3-element list
- a is the first element
- b is the second element
- c is the last.

You can use it anytime you need to deconstruct a value, but also have
a name for the value.

Examples

m at Just j
l@(x:xs)
...




> mind, could you enlighten me?
> Thanks
> Martin
>
>
> On 9/28/2010 5:05 PM, Jonas Almström Duregård wrote:
>>
>> 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
>>>
>>>
>>
>>
>
>
> --
> Martin Tomko
> Postdoctoral Research Assistant
>
> Geographic Information Systems Division
> Department of Geography
> University of Zurich - Irchel
> Winterthurerstr. 190
> CH-8057 Zurich, Switzerland
>
> email:  martin.tomko at geo.uzh.ch
> site:   http://www.geo.uzh.ch/~mtomko
> mob:    +41-788 629 558
> tel:    +41-44-6355256
> fax:    +41-44-6356848
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list