Last active
August 29, 2015 14:17
-
-
Save kjellski/4efa7d8d9b66e59019ae to your computer and use it in GitHub Desktop.
flatten
This file contains 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
type 'a Node = | |
| One of 'a | |
| Many of 'a Node list | |
let testList = [ One "a" ; Many [ One "b" ; Many [ One "c" ; One "d" ] ; One "e" ] ] | |
let rec flatten (list: 'a Node list) : 'a list = | |
let rec flatten_nodes acc (node: 'a Node list) : 'a list = | |
match node with | |
| [] -> acc | |
| One(n)::ns -> n :: flatten_nodes acc ns | |
| Many(n)::ns -> flatten_nodes (flatten_nodes acc n) ns | |
flatten_nodes [] list | |
flatten testList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment