This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.language.implicitConversions | |
| // R is derived from Rational with some fixes. | |
| // https://sites.google.com/site/scalajp/home/documentation/scala-by-example/chapter6 | |
| case class R(n: Int, d: Int) extends Ordered[R] { | |
| private def gcd(x: Int, y: Int): Int = { | |
| if (x == 0) y | |
| else if (x < 0) gcd(-x, y) | |
| else if (y < 0) -gcd( x, -y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed trait Expr | |
| case class N(n: Rational) extends Expr | |
| case class Rational(n: Int = 1, d: Int = 1) extends Expr { | |
| private def gcd(x: Int, y: Int): Int = { | |
| if (x == 0) y | |
| else if (x < 0) gcd(-x, y) | |
| else if (y < 0) -gcd(x, -y) | |
| else gcd(y % x, x) | |
| } | |
| private val g = gcd(n, d) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [0m[[0minfo[0m] [0mLoading project definition from /home/shigemk2/projects/github.com/fpinscala/fpinscala/project[0m | |
| [0m[[0minfo[0m] [0mSet current project to fpinscala (in build file:/home/shigemk2/projects/github.com/fpinscala/fpinscala/)[0m | |
| > [1G[K (reverse-i-search)`': [1G[K (reverse-i-search)`a': answers/console[24G[39G[1G[K (reverse-i-search)`an': answers/console[25G[40G[1G[K (reverse-i-search)`anw': [1G[K > | |
| > answers/console | |
| [0m[[0minfo[0m] [0mStarting scala interpreter...[0m | |
| [0m[[0minfo[0m] [0m[0m | |
| Welcome to Scala version 2.11.5 (OpenJDK 64-Bit Server VM, Java 1.7.0_79). | |
| Type in expressions to have them evaluated. | |
| Type :help for more information. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Rational(n: Int = 1, d: Int = 1) { | |
| private def gcd(x: Int, y: Int): Int = { | |
| if (x == 0) y | |
| else if (x < 0) gcd(-x, y) | |
| else if (y < 0) -gcd(x, -y) | |
| else gcd(y % x, x) | |
| } | |
| private val g = gcd(n, d) | |
| val numer: Int = n/g | |
| val denom: Int = d/g |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/AlgebraCalculation.scala b/AlgebraCalculation.scala | |
| index 691cca8..851311d 100644 | |
| --- a/AlgebraCalculation.scala | |
| +++ b/AlgebraCalculation.scala | |
| @@ -121,7 +121,7 @@ def expand(xs: Expr): Expr = xs match { | |
| case List(x) => List(expand(x)) | |
| case (x::y::xs) => multiply(x,y) :: xs | |
| } | |
| - Mul(f(xs.toList): _*) | |
| + mul(f(xs.toList)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed trait Expr | |
| case class N(n: Int) extends Expr | |
| case class Var(x: String, a: Int, n: Int) extends Expr | |
| case class Add(n: Expr*) extends Expr | |
| case class Mul(n: Expr*) extends Expr | |
| def x(a: Int, n: Int): Var = { | |
| Var("x", a, n) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed trait Expr | |
| case class N(n: Int) extends Expr | |
| case class Var(x: String, a: Int, n: Int) extends Expr | |
| case class Add(n: Expr*) extends Expr | |
| case class Mul(n: Expr*) extends Expr | |
| def x(a: Int, n: Int): Var = { | |
| Var("x", a, n) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed trait Expr | |
| case class N(n: Int) extends Expr | |
| case class Var(x: String, a: Int, n: Int) extends Expr | |
| case class Add(n: Expr*) extends Expr | |
| case class Mul(n: Expr*) extends Expr | |
| def add(xs: List[Expr]): Expr = xs match { | |
| case List() => N(0) | |
| case List(xs) => xs | |
| case xs => Add(xs: _*) // Add(List(N(1), N(2), Var(x,2,2))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed trait Expr | |
| case class N(n: Int) extends Expr | |
| case class Var(x: String, a: Int, n: Int) extends Expr | |
| case class Add(n: Expr*) extends Expr | |
| case class Mul(n: Expr*) extends Expr | |
| def x(a: Int, n: Int): Var = { | |
| Var("x", a, n) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed trait Expr | |
| case class N(n: Int) extends Expr | |
| case class Var(x: String, a: Int, n: Int) extends Expr | |
| case class Add(n: Expr*) extends Expr | |
| case class Mul(n: Expr*) extends Expr | |
| def x(a: Int, n: Int): Var = { | |
| Var("x", a, n) | |
| } |