Last active
May 28, 2017 22:39
-
-
Save abiodun0/4d46553565d2519881a03097e358b3ae to your computer and use it in GitHub Desktop.
pairs [1,2,3,4,5] => [[1,2], [2,3], [3,4], [4,5]]
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
| (partition 2 1 [1, 2, 3, 4, 5]) ;;=> ((1 2) (2 3) (3 4) (4 5)) |
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
| pairs : List x -> List (x, x) | |
| pairs ls = | |
| case ls of | |
| [] -> [] | |
| _ :: [] -> [] | |
| (a::b::xs) -> (a, b) :: pairs (b::xs) |
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
| function pairs (coll) { | |
| if (coll.length < 2) { | |
| return []; | |
| } | |
| const [first, second, ...rest] = coll; | |
| return [[first, second]].concat(pairs([second, ...rest])); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment