Logs: freenode/#haskell
| 2021-04-02 18:02:13 | <StackStackIssue> | to a character, whichr equires it to support the char interface (IIRC). s there some way for me to specify that the stack here is a stack of chars, or otherwise work around the problem? |
| 2021-04-02 18:02:40 | <monochrom> | Oh, I leave C++ for two weeks and it's already rusty. |
| 2021-04-02 18:03:11 | <StackStackIssue> | Yeah 6 months is enough to make Python and Ruby confusing for me, let alone Haskell :p |
| 2021-04-02 18:03:18 | <tomsmeding> | StackStackIssue: what's the error that you're getting? |
| 2021-04-02 18:03:33 | <monochrom> | I left Rust for a year and it's rusty too, but Rust's got to be Rusty... |
| 2021-04-02 18:04:04 | <StackStackIssue> | tomsmeding https://paste.tomsmeding.com/lSDWHpra |
| 2021-04-02 18:04:09 | <tomsmeding> | StackStackIssue: just from looking at your code it seems to me like there should be parentheses around those stackPush and stackPop calls |
| 2021-04-02 18:04:25 | <StackStackIssue> | oh right |
| 2021-04-02 18:04:25 | <tomsmeding> | given that 'helper' seems to take two arguments :) |
| 2021-04-02 18:04:39 | <StackStackIssue> | wasn't there some sort of syntax for avoiding brackets? |
| 2021-04-02 18:04:48 | <StackStackIssue> | Vaguely remember chaining things with dots and $ |
| 2021-04-02 18:04:49 | × | frozenErebus quits (~frozenEre@37.231.244.249) (Ping timeout: 252 seconds) |
| 2021-04-02 18:05:06 | <monochrom> | I think you just need parentheses. helper xs (stackPush stack '{') |
| 2021-04-02 18:05:36 | <tomsmeding> | you _can_ avoid those parentheses, but especially if you're not yet fully up to date with all the syntax, please just write parentheses |
| 2021-04-02 18:05:38 | <tomsmeding> | :) |
| 2021-04-02 18:05:42 | <monochrom> | For example, if you're going for the square root of the sine of x, it's "sqrt (sin x)", not "sqrt sin x". |
| 2021-04-02 18:06:00 | <StackStackIssue> | Yep that's completely fair |
| 2021-04-02 18:06:02 | <StackStackIssue> | No rush |
| 2021-04-02 18:06:03 | <tomsmeding> | regarding the error that you posted: stackPeek returns Maybe Char, not Char https://hackage.haskell.org/package/Stack-0.4.0/docs/Data-Stack.html#v:stackPeek |
| 2021-04-02 18:06:25 | <tomsmeding> | you probably want to scrutinise that using a case-expression |
| 2021-04-02 18:06:45 | <tomsmeding> | i.e. 'case stackPeek stack of ...' |
| 2021-04-02 18:06:59 | → | geekosaur joins (42d52137@66.213.33.55) |
| 2021-04-02 18:08:12 | → | anandprabhu joins (~anand@94.203.250.121) |
| 2021-04-02 18:08:28 | <StackStackIssue> | Perfect, that was my next question |
| 2021-04-02 18:08:46 | <StackStackIssue> | We use the Maybe "wrapper" for when a function may or may not have anything to return, right? |
| 2021-04-02 18:08:55 | <StackStackIssue> | I'm not familiar with Just/Mayve |
| 2021-04-02 18:09:13 | → | ezrakilty joins (~ezrakilty@97-126-95-37.tukw.qwest.net) |
| 2021-04-02 18:09:28 | <tomsmeding> | StackStackIssue: what other language(s) do you know? |
| 2021-04-02 18:09:49 | <tomsmeding> | (perhaps I can compare it with something) |
| 2021-04-02 18:09:49 | <StackStackIssue> | Python/Ruby/Java/C, basics of C++ |
| 2021-04-02 18:10:04 | <tomsmeding> | Java has Optional, I believe; C++ has std::optional since C++17 |
| 2021-04-02 18:10:21 | <tomsmeding> | @src Maybe |
| 2021-04-02 18:10:21 | <lambdabot> | data Maybe a = Nothing | Just a |
| 2021-04-02 18:10:36 | <tomsmeding> | if you have a 'Maybe a', e.g. a 'Maybe Char', then it's either 'Nothing' or 'Just Char' |
| 2021-04-02 18:11:08 | <StackStackIssue> | Right |
| 2021-04-02 18:11:13 | <StackStackIssue> | Either we get it or we don't |
| 2021-04-02 18:11:13 | <tomsmeding> | > case Just 123 of { Nothing -> "it was nothing" ; Just n -> "it was: " ++ show n } |
| 2021-04-02 18:11:15 | <lambdabot> | "it was: 123" |
| 2021-04-02 18:11:20 | <tomsmeding> | > case Nothing of { Nothing -> "it was nothing" ; Just n -> "it was: " ++ show n } |
| 2021-04-02 18:11:21 | <lambdabot> | "it was nothing" |
| 2021-04-02 18:11:29 | <tomsmeding> | (normally you'd write this on multiple lines instead of with the { }) |
| 2021-04-02 18:11:32 | → | bitmapper joins (uid464869@gateway/web/irccloud.com/x-nbhjseffckoxomyo) |
| 2021-04-02 18:11:33 | <StackStackIssue> | Yep! |
| 2021-04-02 18:12:01 | <tomsmeding> | and of course, instead of 'Just 123', you'd write e.g. a variable or a function call, something that returns a Maybe |
| 2021-04-02 18:12:22 | <tomsmeding> | case-analysis on a literal value is not particularly useful, generally :) |
| 2021-04-02 18:13:12 | <tomsmeding> | I believe none of the languages you named have native support for "sum types", as these are called |
| 2021-04-02 18:13:31 | <StackStackIssue> | so here, assuming I have a Just n I'd need to unwrap the n from the Just |
| 2021-04-02 18:13:33 | <tomsmeding> | C++ has std::variant since C++17 but that's not really native, and it's kludgey |
| 2021-04-02 18:13:48 | → | sagax joins (~sagax_nb@213.138.71.146) |
| 2021-04-02 18:13:50 | <StackStackIssue> | Interesting |
| 2021-04-02 18:13:50 | <tomsmeding> | StackStackIssue: in the case branch for Just, you're automatically given the 'n' |
| 2021-04-02 18:14:03 | <StackStackIssue> | Oh I see |
| 2021-04-02 18:14:10 | <StackStackIssue> | Where can i read more on the theory? |
| 2021-04-02 18:14:19 | <tomsmeding> | there is also 'fromJust :: Maybe a -> a', which throws an exception if it happens to be Nothing -- but that's kind of ugly and you generally don't want to use that :p |
| 2021-04-02 18:14:25 | <StackStackIssue> | +1 |
| 2021-04-02 18:14:33 | <tomsmeding> | 99.9% of the cases there is a case expression or something else that fits better |
| 2021-04-02 18:14:35 | <geekosaur> | there's also |
| 2021-04-02 18:14:37 | <geekosaur> | :t maybe |
| 2021-04-02 18:14:38 | <lambdabot> | b -> (a -> b) -> Maybe a -> b |
| 2021-04-02 18:14:52 | <geekosaur> | or the simpler case when there's a default value: |
| 2021-04-02 18:14:56 | <geekosaur> | :t fromMaybe |
| 2021-04-02 18:14:57 | <lambdabot> | a -> Maybe a -> a |
| 2021-04-02 18:15:03 | <tomsmeding> | > maybe "nope" (\x -> "it was: " ++ show x) (Just 123) |
| 2021-04-02 18:15:05 | <lambdabot> | "it was: 123" |
| 2021-04-02 18:15:27 | <StackStackIssue> | what is that first b from in |
| 2021-04-02 18:15:29 | <StackStackIssue> | :t maybe |
| 2021-04-02 18:15:30 | <lambdabot> | b -> (a -> b) -> Maybe a -> b |
| 2021-04-02 18:15:35 | <StackStackIssue> | for*, not from |
| 2021-04-02 18:15:37 | <tomsmeding> | default value |
| 2021-04-02 18:15:41 | <StackStackIssue> | Ah |
| 2021-04-02 18:15:54 | <jumper149> | StackStackIssue: I think this explains it ok: https://wiki.haskell.org/Algebraic_data_type |
| 2021-04-02 18:16:01 | × | geowiesnot quits (~user@i15-les02-ix2-87-89-181-157.sfr.lns.abo.bbox.fr) (Ping timeout: 260 seconds) |
| 2021-04-02 18:16:05 | <jumper149> | It's pretty concise at least |
| 2021-04-02 18:16:10 | <tomsmeding> | scrutinising a 'Maybe a', it's either Nothing (in which case you get the default value, of some type 'b' you can choose), or it's Just x (in which case the 'a -> b' maps that x to a value of the same type 'b') |
| 2021-04-02 18:16:23 | <tomsmeding> | @src maybe |
| 2021-04-02 18:16:23 | <lambdabot> | maybe n _ Nothing = n |
| 2021-04-02 18:16:23 | <lambdabot> | maybe _ f (Just x) = f x |
| 2021-04-02 18:16:33 | <StackStackIssue> | I see |
| 2021-04-02 18:16:34 | <tomsmeding> | well that's a different way to writ it :p |
| 2021-04-02 18:16:36 | × | jonathanx quits (~jonathan@h-176-109.A357.priv.bahnhof.se) (Remote host closed the connection) |
| 2021-04-02 18:16:39 | <StackStackIssue> | we don;t care about the default |
| 2021-04-02 18:16:46 | <tomsmeding> | you can also write: maybe def f m = case m of { Nothing -> def ; Just x -> f x } |
| 2021-04-02 18:16:48 | <StackStackIssue> | since we have maybe _ f (just x) |
| 2021-04-02 18:17:05 | → | jonathanx joins (~jonathan@h-176-109.A357.priv.bahnhof.se) |
| 2021-04-02 18:17:17 | <tomsmeding> | the definition in that @src uses Haskell's definition-by-cases with pattern matching |
| 2021-04-02 18:17:25 | <StackStackIssue> | Thanks. Most of my knowledge is from OOP languages with some functional features from self-teaching and high school course, so this is all uncharted territory! |
| 2021-04-02 18:17:35 | <tomsmeding> | the line where the pattern matches the arguments is chosen |
| 2021-04-02 18:18:00 | <tomsmeding> | i.e. 'maybe d f Nothing' will take the first line and return 'd', whereas 'maybe d f (Just 42)' will take the second line and return 'f 42' |
| 2021-04-02 18:18:18 | × | andi- quits (~andi-@NixOS/user/andi-) (Ping timeout: 268 seconds) |
| 2021-04-02 18:18:29 | <StackStackIssue> | Very nice |
| 2021-04-02 18:18:34 | <tomsmeding> | % foo 1 = 42 ; foo 2 = 100 ; foo n = 0 |
| 2021-04-02 18:18:35 | <yahb> | tomsmeding: |
| 2021-04-02 18:18:37 | <tomsmeding> | % foo 2 |
| 2021-04-02 18:18:37 | <yahb> | tomsmeding: 100 |
| 2021-04-02 18:18:40 | <tomsmeding> | % foo 3 |
| 2021-04-02 18:18:40 | <yahb> | tomsmeding: 0 |
| 2021-04-02 18:18:48 | <tomsmeding> | (read the ;'s as newlines) |
| 2021-04-02 18:18:53 | <StackStackIssue> | +1 |
| 2021-04-02 18:19:15 | <tomsmeding> | same as: foo x = case x of { 1 -> 42 ; 2 -> 100 ; n -> 0 } |
| 2021-04-02 18:19:35 | <tomsmeding> | but writing multiple definitions allows you to simulateneously pattern-match on multiple arguments |
All times are in UTC.