Created
December 13, 2016 06:30
-
-
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.
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
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