Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Created March 28, 2017 17:49
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 ccorcos/caf42ba18a47e0b0f9545e984b38195d to your computer and use it in GitHub Desktop.
Save ccorcos/caf42ba18a47e0b0f9545e984b38195d to your computer and use it in GitHub Desktop.
type result 'a = {mutable list: list 'a};
let firstN (n: int) (l: list 'a) :list 'a => {
let result: result 'a = {list: []};
let start = 0;
let stop = min (n - 1) (List.length l - 1);
for i in start to stop {
result.list = List.append result.list [List.nth l i]
};
result.list
};
firstN 2 [1, 2, 3, 4, 5]; /* => list int: [1, 2] */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment