Pattern matching

View: New views
10 Messages — Rating Filter:   Alert me  

Pattern matching

by Jon Harrop :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message


I'm just trying to come up with examples to test David MacIver's
claim "[Scala]'s pattern matching is really rather cumbersome":

  http://unenterprise.blogspot.com/2008/01/why-not-scala.html

How do you translate this into Scala:

  let f(a, (b, c)) = ((a, b), c)

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e

Re: Pattern matching

by Alex Boisvert-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On 1/5/08, Jon Harrop <jon@...> wrote:

I'm just trying to come up with examples to test David MacIver's
claim "[Scala]'s pattern matching is really rather cumbersome":

   http://unenterprise.blogspot.com/2008/01/why-not-scala.html

How do you translate this into Scala:

  let f(a, (b, c)) = ((a, b), c)

Do you mean something like this?

scala> def f[A,B,C](a: A, bc: Tuple2[B,C]) = ((a,bc._1), bc._2)
f: [A,B,C](A,(B, C))((A, B), C)

alex

Re: Pattern matching

by Jon Harrop :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On Saturday 05 January 2008 14:47:50 Alex Boisvert wrote:

> On 1/5/08, Jon Harrop <jon@...> wrote:
> > I'm just trying to come up with examples to test David MacIver's
> > claim "[Scala]'s pattern matching is really rather cumbersome":
> >
> >   http://unenterprise.blogspot.com/2008/01/why-not-scala.html
> >
> > How do you translate this into Scala:
> >
> >   let f(a, (b, c)) = ((a, b), c)
>
> Do you mean something like this?
>
> scala> def f[A,B,C](a: A, bc: Tuple2[B,C]) = ((a,bc._1), bc._2)
> f: [A,B,C](A,(B, C))((A, B), C)

Hmm, Scala gives a different response for me:

scala> val x = (1, (2, 3))
x: (Int, (Int, Int)) = (1,(2,3))

scala> def f[A,B,C](a: A, bc: Tuple2[B,C]) = ((a,bc._1), bc._2)
f: [A >: ? <: ?,B >: ? <: ?,C >: ? <: ?](A,(B, C))((A, B), C)

and it doesn't seem to work:

scala> f(x)
<console>:6: error: wrong number of arguments for method f: (A,(B, C))((A, B),
C)
  val unnamed0 = f(x)
                  ^
Is that because a 2-argument function and a function accepting a pair are two
different things in Scala?

How do you write "f" as a function accepting a pair?

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e

Re: Pattern matching

by Derek Chen-Becker-2 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Jon Harrop wrote:                   ^
> Is that because a 2-argument function and a function accepting a pair are two
> different things in Scala?
>
> How do you write "f" as a function accepting a pair?
>

It gets ugly:

scala> val x = (1, (2, 3))
x: (Int, (Int, Int)) = (1,(2,3))

scala> def f[A,B,C](abc : Tuple2[A,Tuple2[B,C]]) =
((abc._1,abc._2._1),abc._2._2)
f: [A,B,C]((A, (B, C)))((A, B), C)

Perhaps the most frustrating thing for me is that you appear to have to
use "Tuple2" explicitly:

scala> def f[A,B,C]((a: A, (b : B, c : C))) = ((a,b),c)
<console>:1: error: identifier expected but '(' found.
       def f[A,B,C]((a: A, (b : B, c : C))) = ((a,b),c)
                    ^
<console>:1: error: ':' expected but eof found.
       def f[A,B,C]((a: A, (b : B, c : C))) = ((a,b),c)
                                                       ^
Derek


Re: Pattern matching

by Alex Boisvert-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On 1/5/08, Jon Harrop <jon@...> wrote:
On Saturday 05 January 2008 14:47:50 Alex Boisvert wrote:

> On 1/5/08, Jon Harrop <jon@...> wrote:
> > I'm just trying to come up with examples to test David MacIver's
> > claim "[Scala]'s pattern matching is really rather cumbersome":
> >
> >   http://unenterprise.blogspot.com/2008/01/why-not-scala.html
> >
> > How do you translate this into Scala:
> >
> >   let f(a, (b, c)) = ((a, b), c)
>
> Do you mean something like this?
>
> scala> def f[A,B,C](a: A, bc: Tuple2[B,C]) = ((a,bc._1), bc._2)
> f: [A,B,C](A,(B, C))((A, B), C)

Hmm, Scala gives a different response for me:

scala> val x = (1, (2, 3))
x: (Int, (Int, Int)) = (1,(2,3))

scala> def f[A,B,C](a: A, bc: Tuple2[B,C]) = ((a,bc._1), bc._2)
f: [A >: ? <: ?,B >: ? <: ?,C >: ? <: ?](A,(B, C))((A, B), C)

and it doesn't seem to work:

scala> f(x)
<console>:6: error: wrong number of arguments for method f: (A,(B, C))((A, B),
C)
  val unnamed0 = f(x)
                  ^
Is that because a 2-argument function and a function accepting a pair are two
different things in Scala?

Yes, I believe that was the basis of the complaint.

How do you write "f" as a function accepting a pair?

scala> def f[A,B,C](t: Tuple2[A, Tuple2[B,C]]) = ((t._1, t._2._1), t._2._2)
f: [A,B,C]((A, (B, C)))((A, B), C)

alex



Re: Pattern matching

by Erkki Lindpere-2 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

You can do it like this as well, but it's still somewhat ugly:

scala> def f[A,B,C]( abc:(A,(B,C)) ) = ((abc._1,abc._2._1),abc._2._2)
f: [A,B,C]((A, (B, C)))((A, B), C)

Derek Chen-Becker wrote:

> Perhaps the most frustrating thing for me is that you appear to have to
> use "Tuple2" explicitly:
>
> scala> def f[A,B,C]((a: A, (b : B, c : C))) = ((a,b),c)
> <console>:1: error: identifier expected but '(' found.
>        def f[A,B,C]((a: A, (b : B, c : C))) = ((a,b),c)
>                     ^
> <console>:1: error: ':' expected but eof found.
>        def f[A,B,C]((a: A, (b : B, c : C))) = ((a,b),c)
>                                                        ^
> Derek
>
>  

Re: Pattern matching

by Andrew.Foggin :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Alex Boisvert wrote:
On 1/5/08, Jon Harrop <jon@...> wrote:
On Saturday 05 January 2008 14:47:50 Alex Boisvert wrote:
> On 1/5/08, Jon Harrop <jon@...> wrote:
> > I'm just trying to come up with examples to test David MacIver's
> > claim "[Scala]'s pattern matching is really rather cumbersome":
> >
> >   http://unenterprise.blogspot.com/2008/01/why-not-scala.html
> >
> > How do you translate this into Scala:
> >
> >   let f(a, (b, c)) = ((a, b), c)
>
> Do you mean something like this?
>
> scala> def f[A,B,C](a: A, bc: Tuple2[B,C]) = ((a,bc._1), bc._2)
> f: [A,B,C](A,(B, C))((A, B), C)

Hmm, Scala gives a different response for me:

scala> val x = (1, (2, 3))
x: (Int, (Int, Int)) = (1,(2,3))

scala> def f[A,B,C](a: A, bc: Tuple2[B,C]) = ((a,bc._1), bc._2)
f: [A >: ? <: ?,B >: ? <: ?,C >: ? <: ?](A,(B, C))((A, B), C)

and it doesn't seem to work:

scala> f(x)
<console>:6: error: wrong number of arguments for method f: (A,(B, C))((A, B),
C)
  val unnamed0 = f(x)
                  ^
Is that because a 2-argument function and a function accepting a pair are two
different things in Scala?

Yes, I believe that was the basis of the complaint.

How do you write "f" as a function accepting a pair?

scala> def f[A,B,C](t: Tuple2[A, Tuple2[B,C]]) = ((t._1, t._2._1), t._2._2)
f: [A,B,C]((A, (B, C)))((A, B), C)

alex



Actually the pattern-matching part compares pretty well.  The problem lies in the lack of unification between function args and tuples (which I think could be fixed) and the lack of argument type inference (which AFAIU cannot be fixed)

scala> def f[A, B, C] : ((A, (B, C))) => ((A, B), C) = { case (a, (b, c)) => ((a, b), c) }
f: [A,B,C]((A, (B, C))) => ((A, B), C)

scala> f(1, (2, 3))
res1: ((Int, Int), Int) = ((1,2),3)

--Andrew

Re: Pattern matching

by Geoff Reedy :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message


On Jan 5, 2008, at 7:19 AM, Jon Harrop wrote:

>
> How do you translate this into Scala:
>
>  let f(a, (b, c)) = ((a, b), c)

How about

scala> def f[A,B,C](x: (A,(B,C))) = x match { case (a,(b,c)) =>  
((a,b),c) }
f: [A,B,C]((A, (B, C)))((A, B), C)

scala> f((1,('c',"D")))
res0: ((Int, Char), java.lang.String) = ((1,c),D)

That's a lot closer than anything else that has been offered so far.

Re: Pattern matching

by csar :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Jon Harrop wrote:
On Saturday 05 January 2008 14:47:50 Alex Boisvert wrote:
> On 1/5/08, Jon Harrop <jon@ffconsultancy.com> wrote:
> > I'm just trying to come up with examples to test David MacIver's
> > claim "[Scala]'s pattern matching is really rather cumbersome":
> >
> >   http://unenterprise.blogspot.com/2008/01/why-not-scala.html
> >
> > How do you translate this into Scala:
> >
> >   let f(a, (b, c)) = ((a, b), c)
>
> Do you mean something like this?
>
> scala> def f[A,B,C](a: A, bc: Tuple2[B,C]) = ((a,bc._1), bc._2)
> f: [A,B,C](A,(B, C))((A, B), C)

Hmm, Scala gives a different response for me:

scala> val x = (1, (2, 3))
x: (Int, (Int, Int)) = (1,(2,3))
As tuples aren't parameter lists in Scala, f takes two parameters, thus
val x = (2,3)
val y = 1
f(y,x)

should work fine. We had this discussion on the (missing) equivalence of parameter lists and tuples already. Some months ago I proposed an apply method on tuples to write
val x = (1, (2, 3))
x(f)
but there is an ambiguity that arises when you define f(t:Tuple1[T]) and f(t:T)

Re: Pattern matching

by Martin Odersky :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On Jan 5, 2008 6:16 PM, Geoffrey Reedy <geoff@...> wrote:

>
> On Jan 5, 2008, at 7:19 AM, Jon Harrop wrote:
>
> >
> > How do you translate this into Scala:
> >
> >  let f(a, (b, c)) = ((a, b), c)
>
> How about
>
> scala> def f[A,B,C](x: (A,(B,C))) = x match { case (a,(b,c)) =>
> ((a,b),c) }
> f: [A,B,C]((A, (B, C)))((A, B), C)
>
> scala> f((1,('c',"D")))
> res0: ((Int, Char), java.lang.String) = ((1,c),D)
>
> That's a lot closer than anything else that has been offered so far.
>
That's how I would write it, too.

 -- Martin