Last active
August 17, 2017 01:12
-
-
Save hide1202/05dff9c356d3b51429004e4d93972702 to your computer and use it in GitHub Desktop.
MultiTable, Scala vs F#
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
let padding (n : int) : string = if ((n / 10) = 0) then " " else " " | |
let tableRow row = | |
[1..9] | |
|> List.map (fun x -> row * x) | |
|> List.map (fun x -> padding x + string x) | |
let rowString n = (tableRow n) |> List.fold (+) " " | |
[<EntryPoint>] | |
let main argv = | |
let result = [1..9] |> List.map rowString |> String.concat "\n" | |
printfn "%s" result | |
0 // return an integer exit code |
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
def makeRowSeq(row: Int) = | |
for (col <- 1 to 10) yield { | |
val prod = (row * col).toString | |
val padding = " " * (4 - prod.length) | |
padding + prod | |
} | |
// Returns a row as a string | |
def makeRow(row: Int) = makeRowSeq(row).mkString | |
// Returns table as a string with one row per line | |
def multiTable() = { | |
val tableSeq = // a sequence of row strings | |
for (row <- 1 to 10) | |
yield makeRow(row) | |
tableSeq.mkString("\n") | |
} | |
println("multiTable [" + multiTable + "]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment