Advertisement
Guest User

Call / cc implementation of map and filter functions

a guest
May 9th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.44 KB | None | 0 0
  1. module CallCC =
  2.  
  3.     let rec private mapk k mapper = function
  4.     | [] -> k []
  5.     | h :: t -> t |> mapk (fun x -> k((mapper h) :: x)) mapper
  6.  
  7.     let map = mapk id
  8.  
  9.     let rec private filterk k predicate = function
  10.     | [] -> k []
  11.     | h :: t -> t |> filterk (fun x -> k (if (predicate h) then h :: x else x)) predicate
  12.  
  13.     let filter = filterk id
  14.  
  15. [1..10000]
  16. |> CallCC.map (fun x -> x * x)
  17. |> CallCC.filter (fun x -> (x % 2) = 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement