Skip to content

Instantly share code, notes, and snippets.

@v2m
Last active August 29, 2015 14:03
Show Gist options
  • Save v2m/7c1e07f473bc0cf857bc to your computer and use it in GitHub Desktop.
Save v2m/7c1e07f473bc0cf857bc to your computer and use it in GitHub Desktop.
[<CompiledName("SplitAt")>]
let splitAt (n: int) (list: 'T list) = Microsoft.FSharp.Primitives.Basics.List.splitAt n list
let rec splitAtFreshConsTail cons n l =
match l with
| [] ->
if n <> 0 then failwith "insufficient length"
else
setFreshConsTail cons []
l
| x::xs ->
if n = 0 then
setFreshConsTail cons []
xs
else
let cons2 = freshConsNoTail x
setFreshConsTail cons cons2
splitAtFreshConsTail cons2 (n - 1) xs
let splitAt n l =
if n = 0 then [], l
else
match l with
| [] -> failwith "insufficient length"
| [_] -> if n = 1 then l, [] else failwith "insufficient length"
| x::xs ->
if n = 1 then [x], xs
else
let cons = freshConsNoTail x
let tail = splitAtFreshConsTail cons (n - 1) xs
cons, tail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment