Created
March 26, 2015 12:00
-
-
Save kjellski/7ed6c1ddfe9942a5c674 to your computer and use it in GitHub Desktop.
last_two
This file contains 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
// 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