
Hi, If I have something like data Patootie = Pa Int | Tootie Int and I want to pull out the indices of all elements of a list that have type constructor Tootie, how would I do that? I thought I might be able to use findIndices, but I don't know how to express the predicate.

tootieIndices = findIndices isTootie
where isTootie (Pa _) = False
isTootie (Tootie _) = True
would be my first approach.
/g
On 2/10/06, Creighton Hogg
Hi, If I have something like data Patootie = Pa Int | Tootie Int and I want to pull out the indices of all elements of a list that have type constructor Tootie, how would I do that?
I thought I might be able to use findIndices, but I don't know how to express the predicate. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- We have lingered in the chambers of the sea By sea-girls wreathed with seaweed red and brown Till human voices wake us, and we drown.

Or inline as
findIndices (\x -> case x of Tootie _ -> True; _ -> False) listOfPasAndTooties
There was a recent thread about wanting a more succint way to write this (unary pattern matching): http://thread.gmane.org/gmane.comp.lang.haskell.cafe/11109 If John got his wish, then you could write something like
findIndices (@ Tootie _) listOfPasAndTooties
Maybe this feature will appear in a future Haskell standard? though I don't see anything on the Haskell' wiki about this... Cheers Jared. -- http://www.updike.org/~jared/ reverse ")-:"

On Fri, 10 Feb 2006, Creighton Hogg wrote:
Hi, If I have something like data Patootie = Pa Int | Tootie Int and I want to pull out the indices of all elements of a list that have type constructor Tootie, how would I do that?
I thought I might be able to use findIndices, but I don't know how to express the predicate.
(\p -> case p of {Pa _ -> False; Tootie _ -> True})

Creighton Hogg
data Patootie = Pa Int | Tootie Int and I want to pull out the indices of all elements of a list that have type constructor Tootie, how would I do that?
x = [Pa 3, Tootie 5, Pa 7, Tootie 9, Pa 11] y = [ i | Tootie i <- x ] z = [ i | i@(Tootie _) <- x ] y or z might be helpful. -- Mark

Mark T.B. Carroll wrote:
Creighton Hogg
writes: data Patootie = Pa Int | Tootie Int and I want to pull out the indices of all elements of a list that have type constructor Tootie, how would I do that?
x = [Pa 3, Tootie 5, Pa 7, Tootie 9, Pa 11] y = [ i | Tootie i <- x ] z = [ i | i@(Tootie _) <- x ]
I think this is what the OP wanted: [ i | (i,Tootie _) <- zip [0..] x ] -- Ben

On 10/02/06, Creighton Hogg
Hi, If I have something like data Patootie = Pa Int | Tootie Int and I want to pull out the indices of all elements of a list that have type constructor Tootie, how would I do that?
I thought I might be able to use findIndices, but I don't know how to express the predicate.
Just to clear up a small point, Tootie isn't a type constructor, but a data constructor. ('Maybe' is a type constructor, 'Just' is a data constructor.) You can use list comprehensions with pattern matching to write this fairly succinctly: tootieIndices xs = [i | (Tootie {}, i) <- zip xs [0..]] The {} matches whatever parameters Tootie might have, so this will continue to work even if you later extend the Tootie data constructor with more fields. If you want to match more carefully, you can of course put a variable there. - Cale
participants (7)
-
Ben Rudiak-Gould
-
Cale Gibbard
-
Creighton Hogg
-
Henning Thielemann
-
J. Garrett Morris
-
Jared Updike
-
mark@ixod.org