trying to make sense to example....

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

trying to make sense to example....

by Daryoush Mehrtash-2 :: Rate this Message:

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

I can't figure out how the interpreter implementation finds the "Pair" class in this example:

http://www.scala-lang.org/docu/examples/files/simpleInterpreter.html

any ideas?

Also in methods:

def lookup(x: Name, e: Environment): M[Value] = e match {
case List() => unitM(Wrong)
case Pair(y, b) :: e1 => if (x == y) unitM(b) else lookup(x, e1) }

What exactly happening behind the scene?   How efficient is the lookup method for large lists?

Thanks for your help

Daryoush

Re: trying to make sense to example....

by Eric Torreborre :: Rate this Message:

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

Hi Daryoush,

>I can't figure out how the interpreter implementation finds the "Pair" class
>in this example:

>http://www.scala-lang.org/docu/examples/files/simpleInterpreter.html
>any ideas?

The Pair object comes from the Predef object which is imported in any Scala program:

http://scalasvn.epfl.ch/cgi-bin/viewvc.cgi/scala/trunk/src/library/scala/Predef.scala?view=markup

Eric.


Re: trying to make sense to example....

by Daryoush Mehrtash-2 :: Rate this Message:

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

thanks.

Looking at the class

http://lamp.epfl.ch/~linuxsoft/scala/scala-2.4.0/docs/api/scala/Predef$object.Pair$object.html#unapply%28%28a%2Cb%29%29

Any idea  on how you would use unapply?

On Jan 3, 2008 6:21 PM, Eric Torreborre <etorreborre@...> wrote:

Hi Daryoush,

>I can't figure out how the interpreter implementation finds the "Pair"
class
>in this example:

> http://www.scala-lang.org/docu/examples/files/simpleInterpreter.html
>any ideas?

The Pair object comes from the Predef object which is imported in any Scala
program:

http://scalasvn.epfl.ch/cgi-bin/viewvc.cgi/scala/trunk/src/library/scala/Predef.scala?view=markup

Eric.


--
View this message in context: http://www.nabble.com/trying-to-make-sense-to-example....-tp14609337p14609716.html
Sent from the Scala - Lounge mailing list archive at Nabble.com.




--
Daryoush



Re: trying to make sense to example....

by Arrgh :: Rate this Message:

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

Daryoush Mehrtash wrote:
> Looking at the class
>
> http://lamp.epfl.ch/~linuxsoft/scala/scala-2.4.0/docs/api/scala/Predef$object.Pair$object.html#unapply%28%28a%2Cb%29%29 
> <http://lamp.epfl.ch/%7Elinuxsoft/scala/scala-2.4.0/docs/api/scala/Predef$object.Pair$object.html#unapply%28%28a%2Cb%29%29>
>
> Any idea  on how you would use unapply?
In the following...

> scala> val p = Pair(1,"hi there")
> p: (Int, java.lang.String) = (1,hi there)
>
> scala> def rep(p: Pair[Int, String]) = p match {
>      | case Pair(n,s) => for (i <- 1 to n) println(s) // Pair.unapply
> is called here; explanation below
>      | }
> rep: ((Int, String))Unit
>
> scala> rep(p)
> hi there
>
> scala> rep(Pair(2,"bye"))
> bye
> bye
It took me an hour or so to grok this the first time around... Unapply
is used to implement extractors, namely something that picks apart a
value so that its components can be pattern matched:

val myFoo: Foo = Foo(1,2,3)
myFoo match {
  case Foo(a,b,c) => ...
}

"case Foo(a,b,c)" gets translated into Foo.unapply(myFoo), which must
return either:
- Some((1,2,3)) (i.e. an Option[Tuple3[Int,Int,Int]]), where the
Some(...) indicates a match and the Tuple3 therein contains the
extracted values), or;
- None, indicating no match.

HTH,

-0xe1a