Created
September 8, 2014 15:21
-
-
Save nkoneko/e4aaecea04cdaa4a6330 to your computer and use it in GitHub Desktop.
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
namespace Example | |
module Main = begin | |
open Monad.Maybe; | |
let maybeString (s:string) = | |
if s = null || s.Length = 0 then Nothing | |
else Just s; | |
let a2A (s:string) = | |
maybe { | |
let! a = maybeString s | |
return a.Replace('a', 'A') | |
}; | |
[<EntryPoint>] | |
let main args = | |
match (maybe { | |
let! a = | |
if args = null || args.Length = 0 then Nothing else Just args.[0] | |
let! b = a2A a | |
return b | |
}) with | |
| Just s -> printfn "%s" s | |
| Nothing -> printfn "Nothing.."; | |
0; | |
end;; |
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
FSC = fsharpc | |
TARGET = bin/MaybeExample.exe | |
SOURCES = Maybe.fs Main.fs | |
.PHONY: all | |
all: clean $(TARGET) | |
$(TARGET): $(SOURCES) | |
@[ -d "bin" ] || mkdir bin | |
$(FSC) -o $@ $^ | |
.PHONY: clean | |
clean: | |
@rm -rf bin |
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
namespace Monad | |
module Maybe = begin | |
type Maybe<'a> = | |
| Just of 'a | |
| Nothing | |
; | |
type MaybeMonad() = | |
member this.Bind(x, f) = | |
match x with | |
| Just y -> f y | |
| Nothing -> Nothing; | |
member this.Delay f = f(); | |
member this.Return x = Just x; | |
let maybe = MaybeMonad(); | |
end;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment