Skip to content

Instantly share code, notes, and snippets.

@rwilson
Created November 30, 2010 21:27
Show Gist options
  • Save rwilson/722438 to your computer and use it in GitHub Desktop.
Save rwilson/722438 to your computer and use it in GitHub Desktop.
Pascal's Triangle in scala
// An exercise using scala to output a nicely formatted Pascal's triangle
import scala.math.max
// Define an extractor for Int so we can match below
object Int {
def unapply(s:String) : Option[Int] = try {
Some(s.toInt)
} catch {
case _ : java.lang.NumberFormatException => None
}
}
object Pascal {
val usage = """
Usage: Pascal rows
"""
def main(args:Array[String]) {
if (args.length == 0)
println(usage)
for (a <- args) a match {
case Int(r) => printTriangle(r)
case s : String => println(s + " is not a whole number")
}
}
/**
* Computes the value at row r and column c
*/
def v(r:Int, c:Int) : Int =
if (c == 0)
1
else
v(r, c-1) * (r-c)/c
def printRow(r:Int, seplen:Int) = {
for (col <- 0 to r) {
if (col > 0)
print(" " * seplen)
val n = v(r+1, col)
print((" " * (seplen - n.toString.length)) + n )
}
println()
}
def printTriangle(rows:Int) = {
// Figure out how many spaces to separate numbers
var largest = 1
for (col <- 0 to rows)
largest = max(largest, v(rows+1, col))
val seplen = largest.toString.length
val sep = " " * seplen
for (r <- 0 to rows) {
print(sep * (rows-r))
printRow(r, seplen)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment