Skip to content

Instantly share code, notes, and snippets.

@cowlike
Created February 17, 2021 22:28
Show Gist options
  • Save cowlike/303dca089222044cd33154a1487e315f to your computer and use it in GitHub Desktop.
Save cowlike/303dca089222044cd33154a1487e315f to your computer and use it in GitHub Desktop.
//tail recursive
let part xs =
let rec f acc =
function
| [] -> List.rev acc
| x :: xs -> f ((x, 1 + List.length (List.takeWhile ((=) x) xs)) :: acc) (List.skipWhile ((=) x) xs)
f [] <| List.ofSeq xs
//non-tail recursive
let part' xs =
let rec f =
function
| [] -> []
| x :: xs -> (x, 1 + List.length (List.takeWhile ((=) x) xs)) :: f (List.skipWhile ((=) x) xs)
f <| List.ofSeq xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment