Skip to content

Instantly share code, notes, and snippets.

@RussellAndrewEdson
Last active August 29, 2015 14:12
Show Gist options
  • Save RussellAndrewEdson/181c36fd1928c85abe35 to your computer and use it in GitHub Desktop.
Save RussellAndrewEdson/181c36fd1928c85abe35 to your computer and use it in GitHub Desktop.
Scala code for Pascal's Triangle.
def nextRow(row: List[Int]): List[Int] = (row, (0 :: row)).zipped.map(_ + _)
/* Generating 5 rows of Pascal's triangle. */
var pascal = List[List[Int]]( List(1,0,0,0,0) )
for (i <- 1 to 4) {
pascal = nextRow(pascal.head) :: pascal
}
pascal = pascal.reverse
for (row <- pascal) {
for (element <- row) {
print(element + " ")
}
println
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment