Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. module Main exposing (..)
  2.  
  3. import Html
  4.  
  5.  
  6. type Maybe a
  7. = Just a
  8. | Nothing
  9.  
  10.  
  11. mapMaybe : (a -> b) -> Maybe a -> Maybe b
  12. mapMaybe f perhaps =
  13. case perhaps of
  14. Nothing ->
  15. Nothing
  16.  
  17. Just a ->
  18. Just (f a)
  19.  
  20.  
  21. maybeToList : (a -> b) -> List (Maybe a) -> List b
  22. maybeToList f perhaps =
  23. case perhaps of
  24. x :: xs ->
  25. let
  26. result =
  27. mapMaybe f x
  28. in
  29. case result of
  30. Nothing ->
  31. maybeToList f xs
  32.  
  33. Just a ->
  34. a :: maybeToList f xs
  35.  
  36. [] ->
  37. []
  38.  
  39.  
  40. main : Html.Html a
  41. main =
  42. Html.text <|
  43. case test of
  44. [ 2, 4 ] ->
  45. "pass"
  46.  
  47. _ ->
  48. "fail"
  49.  
  50.  
  51. succ : Int -> Int
  52. succ =
  53. (+) 1
  54.  
  55.  
  56. test : List Int
  57. test =
  58. let
  59. maybe_list =
  60. [ Just 1, Nothing, Just 3, Nothing ]
  61. in
  62. maybeToList succ maybe_list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement