Home freenode/#haskell: Logs Calendar

Logs: freenode/#haskell

←Prev  Next→ 502,152 events total
2020-11-19 06:19:52 <n0042> Where you wrap a function in a "go" function scoped in a "where" statement that requires parameters that don't make sense to call outside the function. Usually to do something in a list-processing kind of way
2020-11-19 06:20:08 <dsal> Oh. Just recursion. :)
2020-11-19 06:20:36 <zyklotomic> I've been struggling to know what to call that practice lol
2020-11-19 06:20:36 <dsal> Usually there's a higher level abstraction that does the thing already
2020-11-19 06:20:42 <n0042> Yes, but in Scheme or something you'd have to wrap it in a macro to make it look decent.
2020-11-19 06:20:57 <zyklotomic> some things are just kinda un-pronuncable in Haskell lol
2020-11-19 06:21:10 <zyklotomic> like all the weird operators and when someone decides to roll their own custom operator too
2020-11-19 06:21:42 <dsal> > maximumOf (folded . _Right) [Left 1, Right 2, Left 3, Right 4, Left 5, Right 3]
2020-11-19 06:21:44 <lambdabot> Just 4
2020-11-19 06:22:35 <dsal> Operators are just functions. The really common ones have fairly common names. Or we just don't say them much.
2020-11-19 06:23:58 <n0042> I am still trying to wrap my head around some of them. You used two I'm unfamiliar with in that snippet you posted: (.) and <$>
2020-11-19 06:24:07 <dsal> > sum (digitToInt <$> "123")
2020-11-19 06:24:09 <lambdabot> 6
2020-11-19 06:24:16 alp joins (~alp@2a01:e0a:58b:4920:d4c7:761f:e715:72ea)
2020-11-19 06:24:17 <dsal> . and $ are *super* common
2020-11-19 06:24:22 <dsal> :t (.)
2020-11-19 06:24:24 <lambdabot> (b -> c) -> (a -> b) -> a -> c
2020-11-19 06:24:25 <zyklotomic> with time
2020-11-19 06:24:25 <dsal> :t ($)
2020-11-19 06:24:27 <lambdabot> (a -> b) -> a -> b
2020-11-19 06:24:41 codeAlways joins (uid272474@gateway/web/irccloud.com/x-rnqcrwigssxuyxdw)
2020-11-19 06:24:42 <dsal> . is just how you compose functions.
2020-11-19 06:24:50 <dsal> @src (.)
2020-11-19 06:24:50 <lambdabot> (f . g) x = f (g x)
2020-11-19 06:24:56 <zyklotomic> <$> is fmap
2020-11-19 06:24:58 <dsal> That's it.
2020-11-19 06:25:05 <n0042> Oh
2020-11-19 06:25:06 <zyklotomic> it is like a generlaized map
2020-11-19 06:25:22 <dsal> IMO, map shouldn't exist, but at least a few people think it's not bad.
2020-11-19 06:25:35 <dsal> > fmap (+1) [1..5]
2020-11-19 06:25:37 <lambdabot> [2,3,4,5,6]
2020-11-19 06:25:42 <dsal> > (+1) <$> [1..5]
2020-11-19 06:25:44 <lambdabot> [2,3,4,5,6]
2020-11-19 06:25:48 <n0042> ($) I have used plenty to get rid of parentheses, but <$> was a new one. That's pretty sweet
2020-11-19 06:26:07 <dsal> $ should be used sparingly. :)
2020-11-19 06:26:13 <dsal> :t ($)
2020-11-19 06:26:14 <lambdabot> (a -> b) -> a -> b
2020-11-19 06:26:16 <dsal> :t (<$>)
2020-11-19 06:26:18 <lambdabot> Functor f => (a -> b) -> f a -> f b
2020-11-19 06:26:20 <zyklotomic> dsal: I find myself using fmap on lists unconsciously, cause you type that before the list and you don't register that you're getting a list mentally yet
2020-11-19 06:26:38 <n0042> <$> is just infix fmap... got it.. that's pretty neat
2020-11-19 06:26:43 <zyklotomic> yup
2020-11-19 06:26:48 <dsal> @src (<$>)
2020-11-19 06:26:48 <lambdabot> f <$> a = fmap f a
2020-11-19 06:27:16 <zyklotomic> but I'm guessing it's existence is legacy cruft
2020-11-19 06:27:17 <n0042> And you said ($) should be used sparingly? I have been using it willy-nilly on the assumption that people don't like parentheses as much as I do. What's the general guideline there?
2020-11-19 06:27:32 <dsal> If you're composing functions, use .
2020-11-19 06:27:53 <zyklotomic> when would you say it's acceptable to use $
2020-11-19 06:28:05 <dsal> At the end of a bunch of .s :)
2020-11-19 06:28:27 <n0042> take 5 $ repeat 10
2020-11-19 06:28:31 <n0042> or something like that
2020-11-19 06:28:38 <n0042> to avoid take 5 (repeat 10) ?
2020-11-19 06:28:43 <dsal> > replicate 5 10
2020-11-19 06:28:45 <lambdabot> [10,10,10,10,10]
2020-11-19 06:29:02 <dsal> : take 5 . repeat
2020-11-19 06:29:07 <dsal> :t take 5 . repeat
2020-11-19 06:29:08 <zyklotomic> (take 5 . repeat) 10 hon oh you beat me to it
2020-11-19 06:29:09 <lambdabot> a -> [a]
2020-11-19 06:29:27 <zyklotomic> but it's not that much cleaner i'm gonna oh, wait ig you can put a $ instead of the paren
2020-11-19 06:29:40 <dsal> > take 5 . repeat $ 10
2020-11-19 06:29:42 <zyklotomic> i'm clearly not a very good user of $ lol
2020-11-19 06:29:42 <lambdabot> [10,10,10,10,10]
2020-11-19 06:29:55 <n0042> Oh nice
2020-11-19 06:29:55 <dsal> Save yo #
2020-11-19 06:29:57 <dsal> Save yo $
2020-11-19 06:29:58 <dsal> damnit
2020-11-19 06:30:12 <n0042> Okay I see how (.) works now
2020-11-19 06:30:21 <zyklotomic> i'm going to go out on a limb though and say that sometimes
2020-11-19 06:30:30 <zyklotomic> take 5 $ repeat 10 is just easier to read
2020-11-19 06:30:50 <zyklotomic> or my brain hasn't been molded enough
2020-11-19 06:31:21 <dsal> > let f = show . sum . take 5 . repeat in f 10
2020-11-19 06:31:23 <lambdabot> "50"
2020-11-19 06:32:36 <n0042> For something a little different, do any of you know if it's true that last is an O(n) function? Like, if you wanted to use a list as a Queue, would it really be O(n) to call last to dequeue?
2020-11-19 06:32:46 <dsal> > let f = nub . sort show . sum . take 11 . repeat in f 13
2020-11-19 06:32:48 <lambdabot> error:
2020-11-19 06:32:48 <lambdabot> • Couldn't match expected type ‘b -> [a2]’ with actual type ‘[a0]’
2020-11-19 06:32:48 <lambdabot> • Possible cause: ‘sort’ is applied to too many arguments
2020-11-19 06:32:51 <dsal> stupid keyboard
2020-11-19 06:32:54 <dsal> > let f = nub . sort . show . sum . take 11 . repeat in f 13
2020-11-19 06:32:57 <lambdabot> "134"
2020-11-19 06:32:58 <n0042> I sort of assumed that the built-in list object would have a tail pointer, but hoogle says that's O(n)
2020-11-19 06:33:17 <dsal> @src (:)
2020-11-19 06:33:17 <lambdabot> Source not found. Listen, broccoli brains, I don't have time to listen to this trash.
2020-11-19 06:33:20 <dsal> boooo
2020-11-19 06:33:26 <dsal> a list is just a head and a tail.
2020-11-19 06:33:29 <n0042> Whoah that bot has atittude
2020-11-19 06:34:02 <dsal> It's a cons list. You said you're familiar with lisp.
2020-11-19 06:34:14 <n0042> Right, a singly-linked list with a head pointer
2020-11-19 06:34:46 <n0042> So to get the last element you would have to traverse all the way down the list, hence O(n).
2020-11-19 06:34:48 × Stanley00 quits (~stanley00@unaffiliated/stanley00) (Remote host closed the connection)
2020-11-19 06:35:21 <n0042> But there's no reason you couldn't have the low-level logic include a tail pointer so that last is O(1).
2020-11-19 06:35:26 <dsal> Lists are great, but if you need other performance characteristics, there are better data structures.
2020-11-19 06:35:37 <dsal> There are many, many reasons you can't do that.
2020-11-19 06:35:42 <dsal> But if you disagree, write one.
2020-11-19 06:35:56 <dsal> Haskell has very little magic. You can write most of what you see.
2020-11-19 06:36:12 <dsal> Even many of the super fancy compiler optimizations are library-definable.
2020-11-19 06:37:04 <n0042> My plan was to use Data.Array but that seems kind of un-functional. What would you suggest?
2020-11-19 06:37:10 × mceier quits (~mceier@89-68-132-187.dynamic.chello.pl) (Ping timeout: 268 seconds)
2020-11-19 06:37:12 <dsal> Vector?
2020-11-19 06:37:16 <dsal> Seq?

All times are in UTC.