Logs: freenode/#haskell
| 2021-03-13 08:10:36 | <dmwit> | Another way to write this that may be more comfortable for you, if you understand `case`, is `getDate v xs = case v of { DbDate date -> xs ++ [date]; _ -> xs ++ [] }`. |
| 2021-03-13 08:10:37 | × | yalm quits (~yalm@unaffiliated/yalm) (Quit: WeeChat 3.0.1) |
| 2021-03-13 08:10:54 | → | xff0x joins (~xff0x@2001:1a81:5243:ae00:7d99:4326:2dd6:91b3) |
| 2021-03-13 08:10:59 | <dmwit> | `xs ++ [date]` appends two lists. The first is `xs`. The second is a list with a single item, namely, `date`. |
| 2021-03-13 08:11:02 | <dmwit> | For example: |
| 2021-03-13 08:11:04 | <magnuscake> | Ah yes that makes it easier |
| 2021-03-13 08:11:08 | <dmwit> | > [5, 7, 100] ++ [3] |
| 2021-03-13 08:11:10 | <lambdabot> | [5,7,100,3] |
| 2021-03-13 08:11:17 | <magnuscake> | Ok |
| 2021-03-13 08:11:34 | <magnuscake> | But isn't xs supposed to be the list that is passed to the function |
| 2021-03-13 08:11:47 | <magnuscake> | Or rather the list the function is applied to |
| 2021-03-13 08:11:51 | <dmwit> | Hm. You say "the function", but there are (at least) two functions here. |
| 2021-03-13 08:12:13 | <magnuscake> | Sorry the main `filterDbDate` |
| 2021-03-13 08:12:18 | <dmwit> | `xs` definitely is not the `[DatabaseItem]` that's supplied to `filterDbDate`. |
| 2021-03-13 08:12:26 | <magnuscake> | I suppose that is where the xs is coming from? |
| 2021-03-13 08:12:54 | <dmwit> | No. The first argument to `getDate` is a list element. The second is some value that's getting accumulated during the fold. It starts as `[]`, and is extended by calling `getDate`. |
| 2021-03-13 08:13:32 | <dmwit> | (precision: the first argument to `getDate` is an element of the list that's supplied to `filterDbDate`) |
| 2021-03-13 08:13:36 | <magnuscake> | Oh |
| 2021-03-13 08:13:57 | <magnuscake> | Yes yes its more clear now |
| 2021-03-13 08:14:08 | <dmwit> | \\o/ |
| 2021-03-13 08:14:21 | <dmwit> | heh, whoops. I grew an extra arm. I'm not very good at IRC |
| 2021-03-13 08:14:51 | <magnuscake> | Lol. Anyways |
| 2021-03-13 08:15:16 | <magnuscake> | Where does this whole pattern of writing the the function this way come from |
| 2021-03-13 08:15:22 | <magnuscake> | As in |
| 2021-03-13 08:15:31 | → | ddellacosta joins (ddellacost@gateway/vpn/mullvad/ddellacosta) |
| 2021-03-13 08:15:41 | <magnuscake> | I don't think I have seen this sort of thing with folds |
| 2021-03-13 08:16:07 | <dmwit> | Can you give a few examples of other things that you would consider to be "this sort of thing"? |
| 2021-03-13 08:16:12 | <dmwit> | I don't really know what that means. |
| 2021-03-13 08:16:23 | <magnuscake> | From what I have learnt, the arguments are all passed in one line, as in with a foldr |
| 2021-03-13 08:16:30 | <magnuscake> | Oh yes just give me a sec |
| 2021-03-13 08:16:33 | <dmwit> | (Maybe also a few negative examples -- i.e. of things that are "not this sort of thing".) |
| 2021-03-13 08:18:42 | <magnuscake> | Maybe its due to my limited knowledge, but here is a simple one: |
| 2021-03-13 08:18:45 | <magnuscake> | foldr const 0 "tacos" |
| 2021-03-13 08:18:57 | <magnuscake> | Now this is easy to understand |
| 2021-03-13 08:19:23 | <magnuscake> | Lets say, we replace const with something we define ourselves to fold the list eventually |
| 2021-03-13 08:19:56 | CMCDragonkai2 | is now known as CMCDragonkai |
| 2021-03-13 08:20:00 | × | ddellacosta quits (ddellacost@gateway/vpn/mullvad/ddellacosta) (Ping timeout: 246 seconds) |
| 2021-03-13 08:20:07 | <magnuscake> | Couldn't we, instead of defining the whole thing as a function instead of using the where ... |
| 2021-03-13 08:20:16 | <magnuscake> | Or is that even possible? |
| 2021-03-13 08:20:52 | <magnuscake> | I hope you get what I am saying |
| 2021-03-13 08:21:04 | <dmwit> | You ask, "Couldn't we, instead of X...?". But usually such a question must go in the form "Couldn't we, instead of X, do Y?". |
| 2021-03-13 08:21:38 | <magnuscake> | lol sorry I meant it that way |
| 2021-03-13 08:22:00 | <dmwit> | ...meant it what way? |
| 2021-03-13 08:22:17 | <magnuscake> | Couldn't we define the whole thing after the `where` in a single function that we pass as the argument to foldr? |
| 2021-03-13 08:22:29 | <dmwit> | Isn't that what was done? |
| 2021-03-13 08:22:45 | <magnuscake> | Is it? |
| 2021-03-13 08:23:11 | <magnuscake> | Ok I guess I was overthinking it |
| 2021-03-13 08:23:19 | <dmwit> | Yes. We defined "the whole thing" (getDate) after the `where` in a single function that we pass as the argument to foldr. |
| 2021-03-13 08:23:42 | <magnuscake> | Oo ok |
| 2021-03-13 08:24:09 | <magnuscake> | Btw what was the easier way you were talking about? |
| 2021-03-13 08:24:22 | <magnuscake> | To write the above? If you don't mind sharing |
| 2021-03-13 08:24:23 | <dmwit> | I don't believe I ever claimed "easier". Only "better". |
| 2021-03-13 08:24:38 | <magnuscake> | Yes better |
| 2021-03-13 08:25:10 | <dmwit> | This implementation is quadratic, because it repeatedly appends to the list. |
| 2021-03-13 08:25:24 | <dmwit> | Additionally, it goes `xs ++ []`, which doesn't do anything, but spends a lot of time. |
| 2021-03-13 08:26:03 | <dmwit> | You can fix those two things in various ways. One way would be `filterDbDate = reverse . foldr getDate [] where getDate (DbDate date) xs = date:xs; getDate _ xs = xs`. |
| 2021-03-13 08:27:00 | <dmwit> | Another would be to use a so-called "difference list", as in `filterDbDate items = foldr getDate id items [] where getDate (DbDate date) f = f . (date:); getDate _ f = f`. |
| 2021-03-13 08:27:51 | <magnuscake> | Hmmm yes that does look better |
| 2021-03-13 08:28:33 | <magnuscake> | I'll def add those those to my notes |
| 2021-03-13 08:28:50 | <dmwit> | I guess there's probably also something with a left fold? IDK. |
| 2021-03-13 08:29:20 | <dmwit> | But my favorite way is still to not use a fold, because the non-fold implementation is so immediately clear to beginner and expert alike. |
| 2021-03-13 08:29:43 | <dmwit> | (without sacrificing performance) |
| 2021-03-13 08:29:49 | <magnuscake> | Hmmm yes but got to flex those foldr skills |
| 2021-03-13 08:29:57 | dmwit | nods |
| 2021-03-13 08:29:58 | <magnuscake> | Its been challenging |
| 2021-03-13 08:30:09 | <magnuscake> | Can't wait to get to monads lol |
| 2021-03-13 08:30:17 | <magnuscake> | Anyways |
| 2021-03-13 08:30:47 | <magnuscake> | That definitely clears it up. Thank you very much for your time dmwit |
| 2021-03-13 08:31:18 | <dmwit> | 👍 |
| 2021-03-13 08:31:35 | × | magnuscake quits (~magnuscak@87-121-92-61.dyn.launtel.net.au) (Quit: Leaving) |
| 2021-03-13 08:34:23 | → | CodeAlways joins (uid272474@gateway/web/irccloud.com/x-jbfyoygjgujgyaip) |
| 2021-03-13 08:34:49 | → | _ht joins (~quassel@82-169-194-8.biz.kpn.net) |
| 2021-03-13 08:36:58 | × | pavonia quits (~user@unaffiliated/siracusa) (Quit: Bye!) |
| 2021-03-13 08:39:05 | → | monadmatt joins (~user@119-17-128-101.771180.mel.nbn.aussiebb.net) |
| 2021-03-13 08:45:09 | → | merijn joins (~merijn@83-160-49-249.ip.xs4all.nl) |
| 2021-03-13 08:45:10 | <olligobber> | @src (++) |
| 2021-03-13 08:45:10 | <lambdabot> | [] ++ ys = ys |
| 2021-03-13 08:45:11 | <lambdabot> | (x:xs) ++ ys = x : (xs ++ ys) |
| 2021-03-13 08:45:11 | <lambdabot> | -- OR |
| 2021-03-13 08:45:11 | <lambdabot> | xs ++ ys = foldr (:) ys xs |
| 2021-03-13 08:45:33 | × | monadmatt quits (~user@119-17-128-101.771180.mel.nbn.aussiebb.net) (Ping timeout: 264 seconds) |
| 2021-03-13 08:48:56 | → | __monty__ joins (~toonn@unaffiliated/toonn) |
| 2021-03-13 08:51:15 | × | Unhammer quits (~Unhammer@gateway/tor-sasl/unhammer) (Remote host closed the connection) |
| 2021-03-13 08:52:22 | → | Unhammer joins (~Unhammer@gateway/tor-sasl/unhammer) |
| 2021-03-13 08:53:25 | → | ddellacosta joins (ddellacost@gateway/vpn/mullvad/ddellacosta) |
| 2021-03-13 08:54:43 | × | drbean quits (~drbean@TC210-63-209-78.static.apol.com.tw) (Quit: ZNC 1.8.2+cygwin2 - https://znc.in) |
| 2021-03-13 08:56:14 | → | monadmatt joins (~user@119-17-128-101.771180.mel.nbn.aussiebb.net) |
| 2021-03-13 08:57:58 | × | ddellacosta quits (ddellacost@gateway/vpn/mullvad/ddellacosta) (Ping timeout: 245 seconds) |
| 2021-03-13 08:58:43 | × | Benzi-Junior quits (~BenziJuni@dsl-149-64-40.hive.is) (Ping timeout: 276 seconds) |
| 2021-03-13 09:00:15 | → | borne joins (~fritjof@200116b86494d20065fd8eaafdc5f06e.dip.versatel-1u1.de) |
| 2021-03-13 09:00:36 | × | monadmatt quits (~user@119-17-128-101.771180.mel.nbn.aussiebb.net) (Ping timeout: 246 seconds) |
| 2021-03-13 09:04:08 | × | stree quits (~stree@68.36.8.116) (Ping timeout: 256 seconds) |
| 2021-03-13 09:04:09 | × | juri_ quits (~juri@178.63.35.222) (Ping timeout: 264 seconds) |
| 2021-03-13 09:05:57 | × | borne quits (~fritjof@200116b86494d20065fd8eaafdc5f06e.dip.versatel-1u1.de) (Ping timeout: 260 seconds) |
| 2021-03-13 09:06:27 | → | borne joins (~fritjof@2a06:8782:ffbb:1337:e811:68d9:a46:a27f) |
| 2021-03-13 09:07:27 | × | jeremybennett quits (~jeremyben@37.120.211.188) (Remote host closed the connection) |
| 2021-03-13 09:08:46 | × | Sgeo quits (~Sgeo@ool-18b98aa4.dyn.optonline.net) (Read error: Connection reset by peer) |
| 2021-03-13 09:13:11 | → | Benzi-Junior joins (~BenziJuni@88-149-64-40.du.xdsl.is) |
| 2021-03-13 09:14:55 | → | magnuscake joins (~magnuscak@87-121-92-61.dyn.launtel.net.au) |
| 2021-03-13 09:16:14 | → | stree joins (~stree@68.36.8.116) |
All times are in UTC.