Skip to content

Instantly share code, notes, and snippets.

@dillonius01
Created August 21, 2020 17:49
Show Gist options
  • Select an option

  • Save dillonius01/7ba3e3b255c1428b5e5e7be5861b463e to your computer and use it in GitHub Desktop.

Select an option

Save dillonius01/7ba3e3b255c1428b5e5e7be5861b463e to your computer and use it in GitHub Desktop.
module Solution
type MultiDimList =
| Value of int
| InnerList of MultiDimList list
let addParens (str: string) =
"[" + str + "]"
let rec multiDimListToString (multiDimList: MultiDimList) =
match multiDimList with
| Value v -> string v
| InnerList innerList ->
innerList
|> List.map multiDimListToString
|> List.reduce (fun acc elem -> acc + ", " + elem)
|> addParens
let rec multiDimSum (multiDimList: MultiDimList) =
match multiDimList with
| Value v -> v
| InnerList innerList ->
innerList
|> List.map multiDimSum
|> List.sum
[<EntryPoint>]
let main argv =
let n1 = Value(1)
let n2 = Value(2)
let n3 = InnerList([n1; n2])
let n4 = Value(4)
let n5 = InnerList([n4; n3])
let sol = multiDimSum n5
let asString = multiDimListToString n5
printfn "multiDimList: %s" asString
printfn "sum: %i" sol
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment