The Artima Developer Community
Sponsored Link

Scala Buzz
The Y Combinator in Scala

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
David Bernard

Posts: 109
Nickname: davidb31
Registered: Dec, 2007

scala-blogs is a group of tutorials and articles about scala
The Y Combinator in Scala Posted: Sep 25, 2008 5:53 PM
Reply to this message Reply

This post originated from an RSS feed registered with Scala Buzz by David Bernard.
Original Post: The Y Combinator in Scala
Feed Title: Scala Blog
Feed URL: http://www.scala-blogs.org/feeds/posts/default?alt=rss
Feed Description: In an effort to realize the "grow together" spirit, the developers of lift have teamed up with other great minds in the Scala community to bring you Scala-Blogs.org. Our mission is to strengthen the community by sharing our experiences and knowledge so that others can learn from our mistakes and triumph along with our successes. At Scala-Blogs.org you will find tutorials and articles written by a growing host of enthusiasts, each with a different background and area of expertise.
Latest Scala Buzz Posts
Latest Scala Buzz Posts by David Bernard
Latest Posts From Scala Blog

Advertisement
A while ago I ran across an implementation of the Y combinator in Java. My first thought was, "Woah, this is cool!", followed immediately by "Woah, this is hideous!"

Below is my port of that code to Scala. Hopefully it's just as cool, and just a little less hideous.

trait BranchType[F, T] extends (BranchType[F, T] => (F => T))

object B {
def apply[F, T](c: BranchType[F, T] => (F => T)) = new BranchType[F, T] {
def apply(b: BranchType[F, T]): F => T = c(b)
}
}

def Y[F, T] = (f: (F => T) => F => T) =>
B[F, T](x => f(x(x)(_)))(B(x => f(x(x)(_))))

val factorial = Y[Int, Int](f => i => if (i <= 0) 1 else f(i - 1) * i)
factorial(6) == 720

Note that it's a straight port of the Java code, so it doesn't make any use at all of Scala's more powerful type system. (It does, however, make liberal use of type inference and syntactic sugar, especially for Function types.) A question for the type experts: can Scala do better with more advanced types?

Read: The Y Combinator in Scala

Topic: Sweet! There is a new Scala Web Framework available! Previous Topic   Next Topic Topic: Woohoo: Mixed Scala and Java projects in Eclipse

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use