Skip to content

Instantly share code, notes, and snippets.

@nkoneko
Created September 8, 2014 15:21
Show Gist options
  • Save nkoneko/e4aaecea04cdaa4a6330 to your computer and use it in GitHub Desktop.
Save nkoneko/e4aaecea04cdaa4a6330 to your computer and use it in GitHub Desktop.
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;;
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
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