Skip to content

Instantly share code, notes, and snippets.

@palladin
Last active July 4, 2023 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palladin/f4d3d8647f2b5db7e77a913e9bca0e10 to your computer and use it in GitHub Desktop.
Save palladin/f4d3d8647f2b5db7e77a913e9bca0e10 to your computer and use it in GitHub Desktop.
dropWhile using foldr
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