Created
April 7, 2016 14:28
-
-
Save fredcy/c746ab5cda42d2042a33ce986298f69b to your computer and use it in GitHub Desktop.
Apply (a -> Maybe b) function to list returning Nothing if any application results in Nothing, else Just (List b)
This file contains 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
import Html exposing (text) | |
data1 = [ 1, 2, 3, 11 ] | |
fn1 x = if x < 12 then Just (x + 100) else Nothing | |
maybeMap : (a -> Maybe b) -> List a -> (Maybe (List b)) | |
maybeMap fn list = | |
case list of | |
[] -> Just [] | |
(h :: t) -> | |
case fn h of | |
Nothing -> Nothing | |
Just hb -> | |
case (maybeMap fn t) of | |
Nothing -> Nothing | |
Just tb -> Just (hb :: tb) | |
main = | |
text <| toString <| maybeMap fn1 [10, 11, 2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment