Last active
August 29, 2015 14:03
-
-
Save v2m/7c1e07f473bc0cf857bc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
[<CompiledName("SplitAt")>] | |
let splitAt (n: int) (list: 'T list) = Microsoft.FSharp.Primitives.Basics.List.splitAt n list |
This file contains hidden or 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
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