Skip to content

Commit 2c31172

Browse files
committed
add Haskell monad category and snippet of using the Maybe monad
1 parent c92a041 commit 2c31172

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Maybe Monad
3+
description: Using the Maybe monad to handle computations that might fail.
4+
author: ACR1209
5+
tags: haskell, monads, maybe
6+
---
7+
8+
```hs
9+
safeDiv :: Int -> Int -> Maybe Int
10+
safeDiv _ 0 = Nothing
11+
safeDiv x y = Just (x `div` y)
12+
13+
main :: IO ()
14+
main = do
15+
let result = do
16+
a <- safeDiv 10 2
17+
b <- safeDiv a 2
18+
return b
19+
print result -- Output: Just 2
20+
```

0 commit comments

Comments
 (0)