ignature for a list of a set length

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

Hi,
On 28 September 2010 15:07, Martin Tomko
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?)
Well if both the producer and the consumer of this value are your own functions, you can use something other than a list. You can also implement a length indexed list, or use such an implementation but it will be overkill for this task, I think. The two options are either implementing a wrapper type with three fields, or using a 3-tuple. I would use a 3-tuple in both the producer and consumer functions. Best, Ozgur

And if that isn't a possibility, you can use pattern matching:
bla (x:y:z:[]) = do something
bla _ = do nothing
On Tue, Sep 28, 2010 at 4:41 PM, Ozgur Akgun
Hi,
On 28 September 2010 15:07, Martin Tomko
wrote: 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?)
Well if both the producer and the consumer of this value are your own functions, you can use something other than a list. You can also implement a length indexed list, or use such an implementation but it will be overkill for this task, I think.
The two options are either implementing a wrapper type with three fields, or using a 3-tuple. I would use a 3-tuple in both the producer and consumer functions.
Best, Ozgur
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

I think it is simpler to use the Maybe type for this. If you only search for ternary relations, you can write a function, which only converts a list to a 3-tuple if it is of length three and otherwise it returns nothing. Then with catMaybes :: [Maybe a] -> [a], you can convert it back to a list with only 3-tuples. import Data.Maybe getLengthThree = catMaybes . fmap toTup where toTup (x:y:z:[]) = Just (x,y,z) toTup _ = Nothing

I like this suggestion, I will give it a go! thanks, and also for the catMaybes function, I needed something like that some days ago. martin On 9/28/2010 5:20 PM, edgar klerks wrote:
I think it is simpler to use the Maybe type for this. If you only search for ternary relations, you can write a function, which only converts a list to a 3-tuple if it is of length three and otherwise it returns nothing. Then with catMaybes :: [Maybe a] -> [a], you can convert it back to a list with only 3-tuples.
import Data.Maybe
getLengthThree = catMaybes . fmap toTup where toTup (x:y:z:[]) = Just (x,y,z) toTup _ = Nothing
-- 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@geo.uzh.ch site: http://www.geo.uzh.ch/~mtomko mob: +41-788 629 558 tel: +41-44-6355256 fax: +41-44-6356848

2010/9/28 Martin Tomko
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@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
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@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@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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (5)
-
David Virebayre
-
edgar klerks
-
Jonas Almström Duregård
-
Martin Tomko
-
Ozgur Akgun