Newest at the top
| 2026-04-06 10:50:59 +0000 | <yahb2> | ab |
| 2026-04-06 10:50:59 +0000 | <ski> | % do putChar 'a'; putChar 'b' |
| 2026-04-06 10:50:51 +0000 | <ski> | you could also use `do', if you prefer |
| 2026-04-06 10:50:40 +0000 | <ski> | so, try combining your `putChar c' with the `acc' action (representing everything you're planning to do with all the previous `Char'acters you've seen, so far, in the list), combining them with the `>>' operator |
| 2026-04-06 10:50:01 +0000 | TimWolla | (~timwolla@2a01:4f8:150:6153:beef::6667) (Quit: Bye) |
| 2026-04-06 10:49:45 +0000 | <ski> | that is a single compound action, that performs those two sub-actions, in that order, when it is executed |
| 2026-04-06 10:49:21 +0000 | <lambdabot> | IO () |
| 2026-04-06 10:49:20 +0000 | <ski> | @type putChar 'a' >> putChar 'b' |
| 2026-04-06 10:49:15 +0000 | <yahb2> | ab |
| 2026-04-06 10:49:15 +0000 | <ski> | % putChar 'a' >> putChar 'b' |
| 2026-04-06 10:49:13 +0000 | merijn | (~merijn@host-cl.cgnat-g.v4.dfn.nl) (Ping timeout: 248 seconds) |
| 2026-04-06 10:49:02 +0000 | <ski> | and the `>>' operator is good for this |
| 2026-04-06 10:48:53 +0000 | <ski> | but that "queue" can just be a single, compound, `IO'-action, that contains all the `putChar' calls, in sequence |
| 2026-04-06 10:48:30 +0000 | <ski> | yes, basically |
| 2026-04-06 10:48:19 +0000 | <ski> | (of course, only if your `IO'-action is a part of a branch of execution that's activated in `main' will it be executed) |
| 2026-04-06 10:48:08 +0000 | <fp`> | Mm so the way to make this folding impl of putStr would be to produce a queue of IO actions |
| 2026-04-06 10:44:43 +0000 | <ski> | the only two ways to *execute* (not evaluate) an `IO'-action is to (a) make it the value of `main' (or a part of that value); or (b) enter it into the interactor (like e.g. GHCi, or in this case the yahb2 bot) |
| 2026-04-06 10:44:18 +0000 | merijn | (~merijn@host-cl.cgnat-g.v4.dfn.nl) merijn |
| 2026-04-06 10:43:30 +0000 | <ski> | evaluation of `IO'-actions only determine *which* action to (later, if at all) to perform. it doesn't actually perform/execute/do the action |
| 2026-04-06 10:43:26 +0000 | <yahb2> | b |
| 2026-04-06 10:43:26 +0000 | <probie> | % [putChar 'a', putChar 'b', putChar 'c'] !! 1 |
| 2026-04-06 10:43:10 +0000 | <lambdabot> | <IO ()> |
| 2026-04-06 10:43:09 +0000 | <probie> | > [putChar 'a', putChar 'b', putChar 'c'] !! 1 |
| 2026-04-06 10:43:03 +0000 | <ski> | until it's *executed* |
| 2026-04-06 10:42:52 +0000 | <probie> | fp`: Because `putChar 'a'` doesn't do anything until it's "evaluated" in the IO monad |
| 2026-04-06 10:42:20 +0000 | <ski> | (but `IO'-actions (in this case, the `putChar' call has type `IO ()') are of an abstract data type, can't really be printed, so i just left the evaluation trace above at showing the `putChar' call) |
| 2026-04-06 10:40:39 +0000 | <ski> | it is evaluated (at the end, after `foldl' returns). and after that, it is executed |
| 2026-04-06 10:40:18 +0000 | <fp`> | Why isn't putChar 'a' evaluated? Is this due to some laziness in evaluation? |
| 2026-04-06 10:38:52 +0000 | <ski> | otherwise you're just throwing away the old value of the accumulator, each time |
| 2026-04-06 10:38:32 +0000 | <ski> | so, you need to combine `acc' with the `putChar' call |
| 2026-04-06 10:37:53 +0000 | <ski> | and then, only at this point, when `foldl' returns, do we actually start to execute the IO-action : putChar 'c' |
| 2026-04-06 10:37:46 +0000 | <fp`> | Mm I see |
| 2026-04-06 10:37:45 +0000 | merijn | (~merijn@host-cl.cgnat-g.v4.dfn.nl) (Ping timeout: 244 seconds) |
| 2026-04-06 10:37:26 +0000 | <ski> | = putChar 'c' |
| 2026-04-06 10:37:19 +0000 | <ski> | = foldl (\acc c -> putChar c) (putChar 'c') [] |
| 2026-04-06 10:37:15 +0000 | <ski> | = foldl (\acc c -> putChar c) (putChar 'b') ['c'] |
| 2026-04-06 10:37:08 +0000 | <ski> | = foldl (\acc c -> putChar c) (putChar 'a') ['b','c'] |
| 2026-04-06 10:36:54 +0000 | <ski> | = foldl (\acc c -> putChar c) (return ()) ['a','b','c'] |
| 2026-04-06 10:36:39 +0000 | <ski> | putStr' ['a','b','c'] |
| 2026-04-06 10:36:15 +0000 | <ski> | fp` : perhaps i could show an example, to make you understand what happens, above |
| 2026-04-06 10:35:33 +0000 | <ski> | however, i would suggest using `foldr' (or `foldM', perhaps) |
| 2026-04-06 10:34:51 +0000 | <ski> | (or `>>=') |
| 2026-04-06 10:34:44 +0000 | <ski> | you need to use `>>' (or `do'-notation) |
| 2026-04-06 10:34:31 +0000 | <ski> | so you're discarding all `c's but the last one |
| 2026-04-06 10:34:04 +0000 | <ski> | you're not using `acc' |
| 2026-04-06 10:33:10 +0000 | <fp`> | putStr' str = foldl (\acc c -> putChar c) (return ()) str |
| 2026-04-06 10:33:05 +0000 | <fp`> | Hey, I tried to create my own implementation of putStr using foldl, but it only prints the last character of the string. Is this because the accumulator never changes value, so ghc optimizes to only run my function on the last value of the [Char]? |
| 2026-04-06 10:32:59 +0000 | merijn | (~merijn@host-cl.cgnat-g.v4.dfn.nl) merijn |
| 2026-04-06 10:29:16 +0000 | fp` | (~user@2001-14ba-6e24-3000--198.rev.dnainternet.fi) |
| 2026-04-06 10:29:07 +0000 | Pixi | (~Pixi@user/pixi) (Ping timeout: 264 seconds) |