Skip to content

Instantly share code, notes, and snippets.

@gofer
Last active December 30, 2015 14:19
Show Gist options
  • Save gofer/7841615 to your computer and use it in GitHub Desktop.
Save gofer/7841615 to your computer and use it in GitHub Desktop.
Sublist
(* 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