Matching constructors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Matching constructors

Creighton Hogg-3
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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Matching constructors

J. Garrett Morris
tootieIndices = findIndices isTootie
    where isTootie (Pa _)     = False
          isTootie (Tootie _) = True

would be my first approach.

 /g

On 2/10/06, Creighton Hogg <[hidden email]> 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.
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> 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.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Matching constructors

Henning Thielemann
In reply to this post by Creighton Hogg-3

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})

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Matching constructors

Jared Updike
In reply to this post by J. Garrett Morris
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 ")-:"
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Matching constructors

Mark T.B. Carroll-2
In reply to this post by Creighton Hogg-3
Creighton Hogg <[hidden email]> 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 ]

y or z might be helpful.

-- Mark

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Matching constructors

Ben Rudiak-Gould
Mark T.B. Carroll wrote:
> Creighton Hogg <[hidden email]> 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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Matching constructors

Cale Gibbard
In reply to this post by Creighton Hogg-3
On 10/02/06, Creighton Hogg <[hidden email]> 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.

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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe