codesnippets:statemonads
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| codesnippets:statemonads [2021/05/04 00:21] – [A decoder] f2b216 | codesnippets:statemonads [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== State monads ====== | ====== State monads ====== | ||
| - | ~~DISCUSSION ~~ | ||
| * inspired by [[http:// | * inspired by [[http:// | ||
| Line 323: | Line 322: | ||
| main = | main = | ||
| do | do | ||
| - | -- MyData | + | -- MyData |
| - | -- + - take the next 4 characters converted to Integer | + | -- + - take the next 4 characters converted to Integer -------------------------------> |
| - | -- / + - take the next 3 Integer | + | -- / + - take the next 3 Integer -------------------------------------------------> |
| - | -- / | + | -- / |
| - | -- / / / + - take the next 2 characters converted to Integer | + | -- / / / + - take the next 2 characters converted to Integer -------> |
| - | -- / | + | -- / |
| - | -- / / / / / + -take the next 11 characters converted to Double | + | -- / / / / / + -take the next 11 characters converted to Double> |
| -- / | -- / | ||
| -- v v v v v v | -- v v v v v v | ||
| - | print ((parse " | + | print ((decode |
| type DecodeState = [Char] -- the remaining chars | type DecodeState = [Char] -- the remaining chars | ||
| class Decoder tValue where | class Decoder tValue where | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| do | do | ||
| nCount <- takeRepetitionCountS | nCount <- takeRepetitionCountS | ||
| - | liValues <- parseNListS | + | liValues <- decodeNListS |
| return liValues | return liValues | ||
| - | | + | |
| - | | + | |
| | lCount > 0 = | | lCount > 0 = | ||
| do | do | ||
| - | mx <- parseMaybeS | + | mx <- decodeMaybeS |
| Sfr.ifJust mx | Sfr.ifJust mx | ||
| {- then -} (\x -> | {- then -} (\x -> | ||
| do | do | ||
| - | liValues <- parseNListS | + | liValues <- decodeNListS |
| return (x : liValues)) | return (x : liValues)) | ||
| {- else -} (return []) | {- else -} (return []) | ||
| Line 363: | Line 362: | ||
| instance Decoder MyData where | instance Decoder MyData where | ||
| - | | + | |
| do | do | ||
| - | mn1 <- parseMaybeS | + | mn1 <- decodeMaybeS |
| Sfr.ifJust mn1 | Sfr.ifJust mn1 | ||
| {- then -} (\n1 -> | {- then -} (\n1 -> | ||
| do | do | ||
| - | ln2 <- parseListS | + | ln2 <- decodeListS |
| - | mn3 <- parseMaybeS | + | mn3 <- decodeMaybeS |
| Sfr.ifJust mn3 | Sfr.ifJust mn3 | ||
| {- then -} (\n3 -> return (Just (MyData n1 ln2 n3))) | {- then -} (\n3 -> return (Just (MyData n1 ln2 n3))) | ||
| Line 378: | Line 377: | ||
| instance Decoder Integer where | instance Decoder Integer where | ||
| - | | + | |
| do | do | ||
| nLength <- takeRepetitionCountS | nLength <- takeRepetitionCountS | ||
| Line 387: | Line 386: | ||
| instance Decoder Double where | instance Decoder Double where | ||
| - | | + | |
| do | do | ||
| nLength <- takeRepetitionCountS | nLength <- takeRepetitionCountS | ||
| Line 484: | Line 483: | ||
| </ | </ | ||
| * executes, with output:< | * executes, with output:< | ||
| - | Just (MyData 4736 [7364537284958, | + | Just (MyData 4736 [7364537284958, |
| </ | </ | ||
| + | |||
| + | |||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/statemonads.1620080510.txt.gz · Last modified: (external edit)
