Cooler 2017-02-20 02:45:45
why do this const <$> Identity [1, 2, 3] <*> Identity [9, 9, 9] when you can do const [1, 2, 3] [9, 9, 9] ?
stefR 2017-02-20 02:46:09
merijn thx
quchen 2017-02-20 02:47:57
Schrostfutz: Congratulations!
quchen 2017-02-20 02:48:13
Schrostfutz: The »bolted on accumulator« is fairly useful to know about :-)
quchen 2017-02-20 02:48:42
Schrostfutz: It's often not as readable as an explicit recursive implementation of the same feature though.
quchen 2017-02-20 02:49:11
Schrostfutz: foldr is a somewhat fundamental function on lists, so relying on the implementation of it isn't bad style.
quchen 2017-02-20 02:49:42
Schrostfutz: foldr captures what it means to consume a list element-wise to build a result.
quchen 2017-02-20 02:50:24
Anything that walks a list element-wise can be written using foldr :-) map, filter, sequence, …
Schrostfutz 2017-02-20 02:50:38
quchen: Okay, thanks again :)
quchen 2017-02-20 02:51:23
Schrostfutz: It's kind of like »relying on the implementation of id« is not bad style when you write »id f x«.
quchen 2017-02-20 02:52:47
I'm not sure whether foldr is unique, but I strongly suspect there is no other (total) function with its type.
quchen 2017-02-20 02:53:14
Well, no other nontrivial ones.
quchen 2017-02-20 02:53:40
foldr f z xs = z -- would also have the right type, but is clearly nonsense.
stefR 2017-02-20 02:54:20
I have another question. During compilation I have tons of this warning for many modules, (not only Control.Monad, also my own modules): The qualified import of 'Control.Monad' is redundant except perhaps to import instances from 'Control.Monad'. To import instances alone, use: import Control.Monad(). I clearly do not understand the warning. What does it mean ?
quchen 2017-02-20 02:55:11
stefR: GHC warns about unnecessary imports. When you don't need anything from Control.Foo, you should not import it. GHC detects this by checking whether you make use of any of the definitions made in the Foo module in your own (importing) module.
quchen 2017-02-20 02:55:38
stefR: For type classes, this mechanism may be undesirable: maybe you're importing the module because it contains an instance definition that you want to use.
stefR 2017-02-20 02:55:53
Ok, so it is a message about unecessary imports. I'm gonna check that immediatly. Thx.
ongy 2017-02-20 02:55:57
so if you used void from Control.Monad before and then removed the use of it, the compiler will warn about an unused import Control.Monad
stefR 2017-02-20 02:56:07
I like this
quchen 2017-02-20 02:56:18
If you *only* want to import instances from a module, you can make this explicit, you can write »import Foo ()«. This imports nothing except the instances, so to speak.
quchen 2017-02-20 02:56:25
(Instances are always exported/imported.)
quchen 2017-02-20 02:56:33
(Known as the open world assumption)
saolof 2017-02-20 02:56:43
What is cheaper, evaluating a thunk or dereferencing a cons?
quchen 2017-02-20 02:57:17
»Dereferencing«? Do you mean »pattern matching on«?
saolof 2017-02-20 02:57:37
right, deconstructing
Cooler 2017-02-20 02:58:11
why doesn't Integer have a monoid?
quchen 2017-02-20 02:58:13
Pattern matching on something may require evaluation of a thunk, so you could say it may be more expensive the first time
quchen 2017-02-20 02:58:32
Cooler: There are two possible Monoids for Integer (sum and product), and by default none is chosen
saolof 2017-02-20 02:58:49
But is calculating something that's easy to evaluate necessarily more expensive than accessing a stored value on the heap?
Cooler 2017-02-20 02:58:57
quchen, that didn't stop list from getting an applicative
quchen 2017-02-20 02:59:26
Cooler: Heh, good point. But there is a law that says that the Applicative has to match the Monad of a type, and there is only one list monad.
quchen 2017-02-20 02:59:41
So here we have something that helps us pick one of the two (equally valid) Applicatives.
ongy 2017-02-20 03:00:19
what are the 2 valid applicatives for lists?
quchen 2017-02-20 03:00:19
saolof: Well, it depends. Pattern matching might be expensive, like in »Just x = expensive« requires a long computation to be done before matching it.
merijn 2017-02-20 03:00:24
quchen: Eh, there's a LOT more monoids for Integer than two :p
quchen 2017-02-20 03:00:36
merijn: I didn't say »exactly« :-รพ
quchen 2017-02-20 03:00:55
ongy: zip lists and umm cartesian-product-lists (or whatever the standard behaviour is called)
AndreasK 2017-02-20 03:01:17
saolof: Not neccesarily, but depends on a lot of factors, from cache hits to what it is your matching on and if you use the variables bound by it (Just _ -> 1 vs Just a -> a)
Schrostfutz 2017-02-20 03:01:35
How do I properly write list literals? This fails: ["foo" "bar"], this works: ("foo":"bar":[])
AndreasK 2017-02-20 03:01:53
Schrostfutz: You forgot the ,
kadoban 2017-02-20 03:01:58
["foo", "bar"]
ongy 2017-02-20 03:02:01
["foo", "baar"]
Tuplanolla 2017-02-20 03:02:08
["foo", "baaar"]
Schrostfutz 2017-02-20 03:02:14
AndreasK, kadoban: ah, thanks
saolof 2017-02-20 03:02:29
But if I write something like lazylist = iterate (*2) and pow2 n = lazylist !! n , will it actually be less expensive the first time I call it?
Schrostfutz 2017-02-20 03:02:30
ongy, Tuplanolla: Those give the wrong result! :P
ongy 2017-02-20 03:02:56
> ["foo", 'b': drop 2 "baar"]
ongy 2017-02-20 03:02:57
better?
lambdabot 2017-02-20 03:03:00
["foo","bar"]
Schrostfutz 2017-02-20 03:03:27
ongy: Yes! And very concise, too :)
saolof 2017-02-20 03:04:21
sorry, lazylist = iterate (*2) 1
AndreasK 2017-02-20 03:05:06
saolof: Unlikely usually the first call is the most expensive one. It's usually only cheaper to recalculate for primitive unboxed operations like adding two unboxed integers and things like that
saolof 2017-02-20 03:06:42
I see
Cooler 2017-02-20 03:07:16
are there default values for types?
Cooler 2017-02-20 03:07:17
*Main> pure 1 :: Constant String Int
Cooler 2017-02-20 03:07:17
Constant {getConstant = ""}
Cooler 2017-02-20 03:07:31
how did it get ""?
saolof 2017-02-20 03:08:08
Is there a lazy datastructure that I can use like lazy linked lists, but with O(1) access to computed values?
ongy 2017-02-20 03:08:26
Cooler: the mempty from monoid is a sort of default value
ongy 2017-02-20 03:08:36
and I think there's a Defaults class aswell
ongy 2017-02-20 03:09:09
> mempty :: String
lambdabot 2017-02-20 03:09:12
""
Tuplanolla 2017-02-20 03:09:34
> def :: String
mroman 2017-02-20 03:09:34
What's the command again to construct a function based on a type?
Cooler 2017-02-20 03:09:34
what does pure have to do with monoid mempty?
lambdabot 2017-02-20 03:09:40
""
Schrostfutz 2017-02-20 03:09:42
I've been writing mostly C++ lately, GHCs error messages are a blessing...
hpc 2017-02-20 03:10:12
Schrostfutz: they really are
ongy 2017-02-20 03:10:24
Cooler: I think djin(n) not sure exactly
ongy 2017-02-20 03:10:37
whoops ^ is for mroman
hpc 2017-02-20 03:10:52
Schrostfutz: my favorite are the language extension errors which tell you exactly what to do to fix it
kadoban 2017-02-20 03:10:58
saolof: Either Data.Array comes to mind or something like Data.Map, but the latter is O(lg n) access, not O(1).
AndreasK 2017-02-20 03:11:02
saolof: Sequence comes probably closest, but it's logarithmic. True O(1) access would require you to convert the structure something that can be offset accessed as you go which sounds possible but probably won't be worth the overhead and complexity
moljac024 2017-02-20 03:11:11
hey guys
moljac024 2017-02-20 03:11:27
is there a blog post or page where i can see an example implementation of future/promise monad?
mroman 2017-02-20 03:11:32
(and mempty is the identity element in the monoid.)
ongy 2017-02-20 03:11:33
Cooler: what's the exact header of the Applicative instance for Constant? If it's something like `instance Monoid a => Monad (Constant a) where` then it probably uses the monoid instance (and therefore mempty)
mroman 2017-02-20 03:11:49
(not really a default value imo)
mroman 2017-02-20 03:11:54
@djinn a -> b -> a
lambdabot 2017-02-20 03:11:54
f a _ = a
mroman 2017-02-20 03:12:03
yep. that's it.
kadoban 2017-02-20 03:12:11
Hmm, Sequence is probably a better suggestion.
Cooler 2017-02-20 03:12:18
ongy, not sure, in this book they actually tell you to write the functor and applicative instances
Cooler 2017-02-20 03:12:24
yourself
ongy 2017-02-20 03:12:39
mroman: it's (ab)used as a way to get a "default" value in a bunch of places
mroman 2017-02-20 03:13:51
ongy: oh. Good to know :)
mroman 2017-02-20 03:14:00
wouln't that break the monoid laws in some instances though?
Cooler 2017-02-20 03:14:31
oh would you look at that, they give you this "instance Monoid a => Applicative (Constant a) where
Cooler 2017-02-20 03:14:31
"
ongy 2017-02-20 03:14:38
monoid shouldn't be defined for that use, but if it exists it works. And probably is useful most of the
Cooler 2017-02-20 03:15:05
they want you to use mempty
Tuplanolla 2017-02-20 03:15:56
:t def -- There's this thing for when things aren't quite monoids, mroman.
lambdabot 2017-02-20 03:15:59
Default a => a
foxbored 2017-02-20 03:16:58
hoi
padre_angolano 2017-02-20 03:17:32
is it possible to install the 'cabal' binary using stack? '--package cabal-install' doesn't seem to do this
kadoban 2017-02-20 03:18:10
padre_angolano: 'stack install cabal-install' works
kadoban 2017-02-20 03:18:27
It will install to ~/.local/bin/ , so make sure that's on your $PATH
padre_angolano 2017-02-20 03:19:09
kadoban: thanks, working now :-)
kadoban 2017-02-20 03:19:27
Anytime.
Schrostfutz 2017-02-20 03:20:10
> groupBy (\a b -> (a `mod` 3) == (b `mod` 3)) [0..10]
lambdabot 2017-02-20 03:20:13
[[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]]
Schrostfutz 2017-02-20 03:20:52
What am I doing wrong here? I'd expect something like [[0, 3, 6, 9], [1, 4, 7, 10], [2, 5, 8]]
kadoban 2017-02-20 03:21:09
Schrostfutz: group/groupBy only looks at adjacent values.
saolof 2017-02-20 03:21:32
Ah, thanks for the advice, I can live with O(log n) access, just not linear ^^
Schrostfutz 2017-02-20 03:21:39
kadoban: Ah, thanks
kadoban 2017-02-20 03:22:34
> groupBy (\a b -> (a `mod` 3) == (b `mod` 3)) . sortBy (comparing (`mod`3)) $ [0..10]
lambdabot 2017-02-20 03:22:38
[[0,3,6,9],[1,4,7,10],[2,5,8]]
kadoban 2017-02-20 03:22:46
Schrostfutz: That's a common fix ^
Cooler 2017-02-20 03:23:00
is there something weird going on? fmap :: (a -> b) -> Constant e a -> Constant e b
Cooler 2017-02-20 03:23:07
fmap _ x = x
Cooler 2017-02-20 03:23:20
x is returned as is but its type changes
hpc 2017-02-20 03:23:29
does that compile?
Schrostfutz 2017-02-20 03:23:49
kadoban: Ty
Cooler 2017-02-20 03:24:46
no it doesn't compile
hpc 2017-02-20 03:25:17
there you go ;)
Cooler 2017-02-20 03:25:33
so you have to unwrap it just to wrap it again
hpc 2017-02-20 03:25:47
what ends up happening is unifying x :: Constant e a and x :: Constant e b gives you a = b
hpc 2017-02-20 03:25:54
which is an invalid equation
hpc 2017-02-20 03:26:16
when you wrap and unwrap it, you never get that equality and it works
Cooler 2017-02-20 03:28:46
so the fmap and <*> are the exact same
Cooler 2017-02-20 03:28:47
fmap _ (Constant a) = Constant a
Cooler 2017-02-20 03:28:51
(<*>) _ (Constant e) = Constant e
Cooler 2017-02-20 03:29:10
is there a way to repeat?
Cooler 2017-02-20 03:29:24
i mean is there a way to say <*> = fmap
Cooler 2017-02-20 03:29:33
the types don't match
Cooler 2017-02-20 03:31:57
(<*>) _ = fmap doesn't work
Cooler 2017-02-20 03:32:18
oh nvm
Cooler 2017-02-20 03:33:42
is there a way?
Cooler 2017-02-20 03:35:09
no there doesn't seem to be
ahihi 2017-02-20 03:38:52
Cooler: you could define something like rewrap :: Constant e a -> Constant e b; rewrap (Constant a) = Constant a and then have fmap _ = rewrap, (<*>) _ = rewrap
ahihi 2017-02-20 03:38:57
but I'm not sure it's worth it :)
mroman 2017-02-20 03:40:04
Are there some clever algorithms to turn a number in base N to base N+1?
ongy 2017-02-20 03:41:37
there's alwasy unsafeCoerce if you want hate :)
hexagoxel 2017-02-20 03:42:49
coerce should be sufficient in this case.
kadoban 2017-02-20 03:44:47
mroman: I suspect not, unless you count the usual base-conversion algorithm as clever.