Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Last active July 24, 2019 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/7488ef0415f10f0d65eac40129aa37a1 to your computer and use it in GitHub Desktop.
Save isaacabraham/7488ef0415f10f0d65eac40129aa37a1 to your computer and use it in GitHub Desktop.
type FileElement =
| Folder of Name : string * Children : FileElement list
| File of {| Name:string; Size : decimal |}
let rec processFileElement element =
match element with
| File details ->
printfn "Oh look, a file called %s of size %MKB!" details.Name details.Size
| Folder (name, children) ->
printfn "I'm entering %s. It has %d direct children." name children.Length
for child in children do
processFileElement child
let data =
Folder("Media", [
File {| Name = "Bar.txt"; Size = 5M |}
Folder ("Music", [
File {| Name = "ASong.mp3"; Size = 450M |}
File {| Name = "AnotherSong.wma"; Size = 400M |}
File {| Name = "AThirdSong.mp3"; Size = 500M |}
Folder ("An Album", [
File {| Name = "ANestedSong.mp3"; Size = 500M |}
])
])
Folder ("Video", [
File {| Name = "AVideo.avi"; Size = 4500M |}
])
])
processFileElement data
(*
I'm entering Media. It has 3 direct children.
Oh look, a file called Bar.txt of size 5KB!
I'm entering Music. It has 4 direct children.
Oh look, a file called ASong.mp3 of size 450KB!
Oh look, a file called AnotherSong.wma of size 400KB!
Oh look, a file called AThirdSong.mp3 of size 500KB!
I'm entering An Album. It has 1 direct children.
Oh look, a file called ANestedSong.mp3 of size 500KB!
I'm entering Video. It has 1 direct children.
Oh look, a file called AVideo.avi of size 4500KB!
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment