Newest at the top
2024-05-16 19:12:28 +0200 | Tuplanolla | (~Tuplanoll@91-159-69-59.elisa-laajakaista.fi) |
2024-05-16 19:12:26 +0200 | ft | (~ft@p508db8fc.dip0.t-ipconnect.de) |
2024-05-16 19:12:03 +0200 | <ski> | so, `(!) . tabulate (0,n)' allocates a single array, and makes a new function that, when called, will indirect through this array before calling the original function |
2024-05-16 19:11:42 +0200 | <Guest13> | ok ok |
2024-05-16 19:11:30 +0200 | <ski> | `(!)' converts back from that array, to the function |
2024-05-16 19:11:19 +0200 | <ski> | `tabulate (0,n)' converts the function to an array that lists the results of the function, for each input in the range `(0,n)' |
2024-05-16 19:10:47 +0200 | <ski> | (and .. it might be unclear whether there'd be only a single call to `memoArray' here, or three separate calls) |
2024-05-16 19:10:45 +0200 | <Guest13> | ! . tabulate |
2024-05-16 19:10:25 +0200 | <ski> | .. but my unintended usage of view patterns, above, doesn't support this style of defining `fib' |
2024-05-16 19:10:24 +0200 | <Guest13> | what does memoArray do again? |
2024-05-16 19:09:55 +0200 | <ski> | (memoArray (0,n) -> fib) n = fib (n-1) + fib (n-2) |
2024-05-16 19:09:51 +0200 | <ski> | (memoArray (0,n) -> fib) 1 = 1 |
2024-05-16 19:09:48 +0200 | <ski> | (memoArray (0,n) -> fib) 0 = 0 |
2024-05-16 19:09:35 +0200 | <ski> | it would perhaps be even more fun, if we could write |
2024-05-16 19:09:17 +0200 | <ski> | well, we're also avoiding wrapping the whole `\case ...' in brackets. so when we break that over multiple lines in the source file, we don't need a closing bracket at the end, that may look a bit unclear which opening bracket it is matching, multiple lines up |
2024-05-16 19:08:58 +0200 | <Guest13> | you are saying that to get x in (f -> x) = y you need to call f on y |
2024-05-16 19:08:38 +0200 | <Guest13> | I think I get it now though |
2024-05-16 19:08:31 +0200 | <Guest13> | I understood the below one immediately but the top was super confusing lol |
2024-05-16 19:08:08 +0200 | <Guest13> | lol |
2024-05-16 19:08:04 +0200 | <ski> | cuteness |
2024-05-16 19:07:59 +0200 | <Guest13> | is there a reason to phrase it like that? |
2024-05-16 19:07:36 +0200 | <ski> | fib = memoArray (0,n) (\case 0 -> 0; 1 -> 1; n -> fib (n-1) + fib (n-2)) |
2024-05-16 19:07:26 +0200 | <ski> | this is actually the same thing as |
2024-05-16 19:07:20 +0200 | <ski> | (memoArray (0,n) -> fib) = \case 0 -> 0; 1 -> 1; n -> fib (n-1) + fib (n-2) |
2024-05-16 19:07:11 +0200 | <ski> | so, in my example |
2024-05-16 19:07:02 +0200 | <ski> | (input `x', matched against the view-pattern `(f -> y)', will call `f' on the input `x', and match the result of that to the pattern `y') |
2024-05-16 19:07:01 +0200 | <Guest13> | I see how applying f to both sides does that |
2024-05-16 19:06:32 +0200 | <ski> | y = f x |
2024-05-16 19:06:28 +0200 | <ski> | amounts to the same thing as |
2024-05-16 19:06:23 +0200 | <ski> | (f -> y) = x |
2024-05-16 19:06:19 +0200 | <ski> | or, more generally, we can say that |
2024-05-16 19:05:55 +0200 | <ski> | (.. except that if `y' is a complex pattern, rather than a simple variable, then failure to match that pattern will cause the whole defining equation `foo (f -> y) = ...' to fail (trying the next defining equation instead), while with the `where'-version above, this doesn't happen) |
2024-05-16 19:05:01 +0200 | <ski> | y = f x |
2024-05-16 19:04:58 +0200 | <ski> | where |
2024-05-16 19:04:56 +0200 | <ski> | foo x = ..y.. |
2024-05-16 19:04:49 +0200 | <ski> | can be refactored/rewritten as |
2024-05-16 19:04:41 +0200 | <Guest13> | yeah |
2024-05-16 19:04:36 +0200 | <ski> | foo (f -> y) = ..y.. |
2024-05-16 19:04:25 +0200 | <ski> | in general |
2024-05-16 19:04:25 +0200 | <Guest13> | but not how the syntax reflects that |
2024-05-16 19:04:22 +0200 | <ski> | oh |
2024-05-16 19:04:17 +0200 | <Guest13> | I understand what it is doing |
2024-05-16 19:03:56 +0200 | <ski> | that example makes sense to you ? |
2024-05-16 19:03:31 +0200 | <ski> | this will call `reverse' on the input `[2,3,5,7]', before trying to match it with the pattern `x:_'. so we actually match the list `[7,5,3,2]' with the pattern `x:_', so `x' becomes `7' |
2024-05-16 19:02:54 +0200 | <lambdabot> | 7 |
2024-05-16 19:02:52 +0200 | <ski> | > let last (reverse -> x:_) = x in last [2,3,5,7] |
2024-05-16 19:02:31 +0200 | <ski> | e.g. |
2024-05-16 19:02:24 +0200 | <ski> | my use of the view pattern above is rather unusual .. normally view patterns are used in function (formal) parameter patterns |
2024-05-16 19:02:12 +0200 | <Guest13> | we are assigning a function to the left |
2024-05-16 19:01:50 +0200 | <Guest13> | I don't understand the assignment |