(set! %load-path (cons "." %load-path)) (use-modules (monad maybe) (monad either) (monad state)) (define (test name expr expectation) (let ((res (eval expr (interaction-environment)))) (if (equal? res expectation) (format #t "[~s] passed\n" name) (format #t "[~s] ERROR: expected: ~s, but got: ~s\n" name expectation res)))) (define (f x) (if (> x 0) (Just x) (Nothing))) (begin (test "maybe 1" '(with-monad maybe-monad (a <- (Just 5)) (b <- (f 2)) (c <- (Just 10)) (return (+ a b c))) (Just 17)) (test "maybe 2" '(with-monad maybe-monad (a <- (Just 5)) (b <- (f 0)) (c <- (Just 10)) (return (+ a b c))) (Nothing)) (test "state 1" '(run-state 0 (with-monad state-monad (put 10) (put 9) (modify (lambda (x) (+ 2 x))) (modify (lambda (x) (+ 2 x))) (get))) 13) (test "state 2" '(run-state 10 (with-monad state-monad (get))) 10) (test "either 1" '(with-monad either-monad (a <- (Right 5)) (b <- (Right 0)) (c <- (Right 10)) (return (+ a b c))) (Right 15)) (test "either 2" '(with-monad either-monad (a <- (Right 5)) (b <- (Left "an error")) (c <- (Right 10)) (return (+ a b c))) (Left "an error")))