Skip to content

Instantly share code, notes, and snippets.

@kjellski
Created March 26, 2015 12:00
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 kjellski/7ed6c1ddfe9942a5c674 to your computer and use it in GitHub Desktop.
Save kjellski/7ed6c1ddfe9942a5c674 to your computer and use it in GitHub Desktop.
last_two
// selfmade :/ not yet...
let rec last_two = function
| [] -> None
| [x] -> None
| x::xs ->
if xs.Length > 1 then
last_two xs
else
Some (x, xs.Head)
// better from solution
let rec last_two = function
| [] -> None
| [x] -> None
| [x;xs] -> Some (x, xs)
| x::xs -> last_two xs
last_two [ "a" ; "b" ; "c" ; "d" ];;
// (string * string) option = Some ("c", "d")
last_two [ "a" ];;
// (string * string) option = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment