confusing:lyahpop
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| confusing:lyahpop [2021/03/23 03:10] – created f2b216 | confusing:lyahpop [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Confusing pop statement in LYAH ====== | ||
| + | ~~DISCUSSION~~ | ||
| + | Nothing is perfect. Even though a tremendous effort must have been taken to create LYAH )< | ||
| + | - which does not really make sense | ||
| + | - hence, generates warnings | ||
| + | - it also generates compiler errors, because of incompatibilities with new packages philosophies | ||
| + | - can generate runtime errors if the code would be extendend | ||
| - | <WRAP center round box 60%> | + | LYAH )< |
| + | |||
| + | |||
| + | <WRAP center round box 62%> | ||
| + | |||
| + | <code Haskell> | ||
| + | import Control.Monad.State | ||
| + | |||
| + | pop :: State Stack Int | ||
| + | pop = State $ \(x:xs) -> (x, | ||
| + | |||
| + | push :: Int -> State Stack () | ||
| + | push a = State $ \xs -> ((), | ||
| + | </ | ||
| + | |||
| + | <WRAP center round box 100%> | ||
| pop is already a stateful computation and push takes an Int and returns a stateful computation. Now we can rewrite our previous example of pushing 3 onto the stack and then popping two numbers off like this: | pop is already a stateful computation and push takes an Int and returns a stateful computation. Now we can rewrite our previous example of pushing 3 onto the stack and then popping two numbers off like this: | ||
| </ | </ | ||
| - | |||
| - | <code > | + | < |
| - | import Control.Monad.State | + | import Control.Monad.State |
| - | + | ||
| - | stackManip :: State Stack Int | + | stackManip :: State Stack Int |
| - | stackManip = do | + | stackManip = do |
| - | push 3 | + | push 3 |
| - | a <- pop | + | a <- pop |
| - | pop | + | pop |
| - | <> | + | </code> |
| + | |||
| + | </ | ||
| + | |||
| + | - The line '' | ||
| + | - Hence, it creates compiler warning: | ||
| + | * < | ||
| + | app\Main.hs: | ||
| + | Defined but not used: `c' | ||
| + | | | ||
| + | mm | c <- pop | ||
| + | | ||
| + | app\Main.hs: | ||
| + | A do-notation statement discarded a result of type `Int' | ||
| + | Suppress this warning by saying `_ <- pop' | ||
| + | | | ||
| + | nn | pop | ||
| + | | ||
| + | Linking .stack-work\... | ||
| + | </ | ||
| + | - The new package philosophy renamed the data type State to StateT (The StateT monad transformer). | ||
| + | * < | ||
| + | app\Main.hs: | ||
| + | Not in scope: data constructor `S.State' | ||
| + | Perhaps you meant one of these: | ||
| + | `S.StateT' | ||
| + | variable `S.state' | ||
| + | Module `Control.Monad.State' | ||
| + | | | ||
| + | 21 | push a = S.State $ \xs -> ((),a:xs) | ||
| + | | ||
| + | </ | ||
| + | - If you use to much '' | ||
| + | * ...-exe...: app\Main.hs: | ||
| + | * which is this line '' | ||
| + | |||
| + | ---- | ||
| + | |||
| + | |||
| + | LYAH )< | ||
confusing/lyahpop.1616465417.txt.gz · Last modified: (external edit)
