Last active
December 30, 2015 14:19
-
-
Save gofer/7841615 to your computer and use it in GitHub Desktop.
Sublist
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
(* sublist *) | |
fun sublist _ _ nil = nil | |
| sublist 0 0 (x::xs) = [x] | |
| sublist 0 e (x::xs) = x::(sublist 0 (e-1) xs) | |
| sublist b e (x::xs) = let | |
val b_ = b + (if b < 0 then (length (x::xs)) else 0) | |
in | |
sublist (b_-1) (e-1) xs | |
end; | |
sublist 2 5 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
(* val it = [2,3,4,5] : int list *) | |
sublist ~6 7 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
(* val it = [4,5,6,7] : int list *) | |
(* filter *) | |
fun filter _ [] = [] | |
| filter pred (x::xs) = | |
let | |
val ys = filter pred xs | |
in | |
if pred x then x::ys else ys | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment