Last active
July 4, 2023 12:41
-
-
Save palladin/f4d3d8647f2b5db7e77a913e9bca0e10 to your computer and use it in GitHub Desktop.
dropWhile using foldr
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
let dropWhile : ('a -> bool) -> list<'a> -> list<'a> = fun pred xs -> | |
let (_, ys) = List.foldBack (fun x (xs, ys) -> let xs' = x :: xs in if pred x then xs', ys else xs', xs') xs ([], []) | |
ys | |
// example 1 | |
[1..10] |> dropWhile (fun x -> x <= 5) // [6..10] | |
// example 2 | |
[1..10] |> dropWhile (fun x -> false) // [1..10] | |
// example 3 | |
[1..10] |> dropWhile (fun x -> true) // [] | |
// example 4 | |
[1; 6; 1; 7] |> dropWhile (fun x -> x <= 5) // [6; 1; 7] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment