Skip to content

Instantly share code, notes, and snippets.

@shonfeder
Created December 13, 2016 06:30
Show Gist options
  • Save shonfeder/5c8d07542a0ac179877d8a4be59de996 to your computer and use it in GitHub Desktop.
Save shonfeder/5c8d07542a0ac179877d8a4be59de996 to your computer and use it in GitHub Desktop.
The maybe monad, hard coded in Prolog, just for fun and IRC play time.
maybe(just(_)).
maybe(nothing).
bind(nothing, _, nothing).
bind(just(A), P, M) :- call(P, A, M),
maybe(M).
return(X, just(X)).
maybe_div(_,0,nothing) :- !.
maybe_div(A,B,just(C)) :- B =\= 0,
C is A / B.
/*
?- return("Testing!", X).
X = just("Testing!").
?- return(just("Hi #logic! I'm nested!"), X).
X = just(just("Hi #logic! I'm nested!")).
?- bind(just(20), maybe_div(100), M).
M = just(5).
?- bind(just(0), maybe_div(20), M).
M = nothing.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment