Skip to content

Instantly share code, notes, and snippets.

@hide1202
Last active August 17, 2017 01:12
Show Gist options
  • Save hide1202/05dff9c356d3b51429004e4d93972702 to your computer and use it in GitHub Desktop.
Save hide1202/05dff9c356d3b51429004e4d93972702 to your computer and use it in GitHub Desktop.
MultiTable, Scala vs F#
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
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