Search Haskell Channel Logs

Sunday, January 29, 2017

#haskell channel featuring kadoban, akfp, MarcelineVQ, sergisiberia2003, ph88, lambdabot, and 8 others.

ph88 2017-01-29 08:48:09
ertes, can i use reverse ?
Gurkenglas 2017-01-29 08:48:25
:t \foo -> filter foo . sequence -- How do you correctly backtrack through an exponential space? Requesting a version of this that exploits that foo x implies foo for each prefix of x.
lambdabot 2017-01-29 08:48:27
Traversable t => (t a -> Bool) -> t [a] -> [t a]
Gurkenglas 2017-01-29 08:50:35
Huh, that nonspecialization of t to [] means that I might need to rephrase that "prefix" stuff
pikajude 2017-01-29 08:51:16
filter needs []
pikajude 2017-01-29 08:51:18
iirc
Gurkenglas 2017-01-29 08:52:17
pikajude, I mean t. filter, as lambdabot shows, already forces the applicative to []
pikajude 2017-01-29 08:52:27
oh
lpaste 2017-01-29 08:55:05
xcmw pasted "ZipList with ApplicativeDo" at http://lpaste.net/351771
xcmw 2017-01-29 08:57:02
Why do the code error? ZipList is an applicative and I enabled ApplicativeDo.
kadoban 2017-01-29 08:58:59
It probably isn't matching one of the cases that ApplicativeDo handles. I'm still not clear on what exact form(s) it can do.
sergisiberia2003 2017-01-29 08:59:35
how to tuples?
kadoban 2017-01-29 08:59:52
> (5, "stuff")
lambdabot 2017-01-29 08:59:54
(5,"stuff")
sergisiberia2003 2017-01-29 09:00:06
can I have fraction?
Gurkenglas 2017-01-29 09:00:26
:t \foo -> foldlM (\accum factor -> [new | x <- factor, let new = accum ++ [x], foo new]) [] -- If t is set to [], this applies, but the ++ [_] makes me think its not the canonical way.
lambdabot 2017-01-29 09:00:28
Foldable t => ([a] -> Bool) -> t [a] -> [[a]]
ertes 2017-01-29 09:00:51
ph88: ideally don't use anything
sergisiberia2003 2017-01-29 09:00:54
thank you lambdabot, but how to substract fraction if fraction is negative?
ertes 2017-01-29 09:01:07
ph88: really just write the most straightforward implementation you can think of… use recursion
ph88 2017-01-29 09:01:23
yeah i had to look at the implementation of foldr :/
xcmw 2017-01-29 09:01:26
sergisiberia2003: look into Rational
sergisiberia2003 2017-01-29 09:01:45
xcmw: ok, is substract negative possibility?
xcmw 2017-01-29 09:02:09
> (1 % 2) - (-1 % 3)
lambdabot 2017-01-29 09:02:11
5 % 6
sergisiberia2003 2017-01-29 09:02:19
sorry for bad english, i'm from pakistan! I very much enjoy hashel
sergisiberia2003 2017-01-29 09:02:25
thank you for help.
ph88 2017-01-29 09:02:46
how can i hide function from the prelude individually
sergisiberia2003 2017-01-29 09:03:00
i believe I have an error
sergisiberia2003 2017-01-29 09:03:04
goodbye
ertes 2017-01-29 09:03:04
ph88: import Prelude hiding (blah)
geekosaur 2017-01-29 09:03:06
import Prelude hiding (...)
Gurkenglas 2017-01-29 09:03:18
ph88, I don't think it counts if you just paste the foldr source in and refactor until we can't tell :D
ertes 2017-01-29 09:03:38
ph88: an explicit Prelude import overrides the implicit one
geekosaur 2017-01-29 09:03:40
if you import Prelude explicitly, it overrides the implicit full import
geekosaur 2017-01-29 09:03:42
heh
ertes 2017-01-29 09:03:46
=)
MarcelineVQ 2017-01-29 09:04:03
xcmw: ApplicativeDo is reeeeally picky, don't use $ on your return/pure
ertes 2017-01-29 09:05:09
ph88: i think you're overthinking this… i *really* want you to write straightforward implementations… just time-travel to the point when you just started with haskell and were learning about basic list functions and recursion
ertes 2017-01-29 09:05:27
that's the kind of implementation i'd like you to write
ph88 2017-01-29 09:05:46
well that part was always a bit unclear so i kinda moved on
ph88 2017-01-29 09:06:14
so far i have this go [x1] s2 = x1 : s2 go (x1:xs1) s2 = go x1 (go xs1 s2)
ertes 2017-01-29 09:06:15
i see… then it's time to catch up =)
ertes 2017-01-29 09:06:29
ph88: what about the empty list?
xcmw 2017-01-29 09:06:35
MarcelineVQ: It works now. Thanks
ph88 2017-01-29 09:07:20
yeah i can add them but the error seems the same anyway
ertes 2017-01-29 09:07:49
ph88: what's the head of ((x : xs) ++ ys)?
ph88 2017-01-29 09:07:57
x
ertes 2017-01-29 09:08:02
ph88: and the tail?
ph88 2017-01-29 09:08:09
xs ++ ys
ertes 2017-01-29 09:08:12
there you go =)
ertes 2017-01-29 09:08:23
(x : xs) ++ ys = x : (xs ++ ys)
ph88 2017-01-29 09:09:04
i thought that's what i have now without infix notation
ertes 2017-01-29 09:09:18
nope
ertes 2017-01-29 09:09:27
you have a 'go' there that should really be (:)
ph88 2017-01-29 09:09:36
yeah i removed that go
ph88 2017-01-29 09:10:03
https://paste.fedoraproject.org/540197/48572059/ this is what i have now
Gurkenglas 2017-01-29 09:10:13
:t filter foo . sequence -- Is there a version of this that exploits that foo can say False as soon as no path to True remains?
lambdabot 2017-01-29 09:10:16
error:
lambdabot 2017-01-29 09:10:17
• Variable not in scope: foo :: t a -> Bool
lambdabot 2017-01-29 09:10:17
• Perhaps you meant 'for' (imported from Data.Traversable)
ph88 2017-01-29 09:10:37
no that go i had before should be there !
Gurkenglas 2017-01-29 09:10:46
:t \foo -> filter foo . sequence -- you forget querying to lambdabot first _one time_...
lambdabot 2017-01-29 09:10:52
Traversable t => (t a -> Bool) -> t [a] -> [t a]
ertes 2017-01-29 09:10:55
ph88: *something* should be there, but not 'go'
ph88 2017-01-29 09:11:27
ah ok i got it now
ph88 2017-01-29 09:13:20
ertes, what did you mean with a common pattern ?
ph88 2017-01-29 09:13:34
repetition in a list ?
ertes 2017-01-29 09:13:34
ph88: i can't explain until you have done the first two steps
ph88 2017-01-29 09:13:52
the 2nd step i knew right away so i moved on to the 3rd
ertes 2017-01-29 09:13:58
do it anyway
ph88 2017-01-29 09:14:03
do what ?
ertes 2017-01-29 09:14:06
step 2
ph88 2017-01-29 09:14:11
yes it's done
ertes 2017-01-29 09:14:19
ok, can you paste your code?
ph88 2017-01-29 09:14:23
ok
ph88 2017-01-29 09:14:44
https://paste.fedoraproject.org/540212/72087914/
Gurkenglas 2017-01-29 09:15:18
mapM_ is cheating too
ph88 2017-01-29 09:15:34
o_O
ertes 2017-01-29 09:15:39
ph88: question: go [] ys = ys; go (x : xs) ys = x : go xs ys
ertes 2017-01-29 09:15:47
ph88: are there any unhandled cases?
ertes 2017-01-29 09:15:54
with my two-clause version that is
ph88 2017-01-29 09:16:06
nope :P
Gurkenglas 2017-01-29 09:16:10
"without combinators like 'traverse'" <- mapM = traverse, mapM_ = traverse_
ph88 2017-01-29 09:16:37
didn't know mapM_ was traverse_
ertes 2017-01-29 09:16:39
ph88: that's right… adjust your definition of 'go' and then do 'prt' without cheating =)
ertes 2017-01-29 09:16:56
again using recursion
ph88 2017-01-29 09:20:17
prt [] = putStrLn "" prt (x:xs) = putStrLn x >> prt xs
ertes 2017-01-29 09:20:44
ph88: can you do it without the extra line at the end?
ph88 2017-01-29 09:21:03
no, ghc will throw an exception that i have non-exhaustive pattern match
ph88 2017-01-29 09:21:20
i tried that first
ertes 2017-01-29 09:21:23
ph88: for an empty list the resulting action should be an action that does nothing
ertes 2017-01-29 09:21:36
that's not the same as not defining what happens for the empty list
ph88 2017-01-29 09:21:37
i don't know of any action that doesn't do anything
ertes 2017-01-29 09:21:52
you do… you've used it in the past =)
c_wraith 2017-01-29 09:21:53
return builds the action that doesn't do anything
ph88 2017-01-29 09:22:15
ah yes i got some inspiration :P
c_wraith 2017-01-29 09:22:15
that's true of every monad. That's more or less what the monad laws say. :)
ph88 2017-01-29 09:22:26
prt [] = return ()
ertes 2017-01-29 09:22:33
great
ertes 2017-01-29 09:22:52
ph88: now look at the two functions you have defined… we can talk about patterns now: *code* patterns
ertes 2017-01-29 09:22:55
do you see one?
ph88 2017-01-29 09:23:11
cons and recursion
ertes 2017-01-29 09:23:30
yeah… both have some base case for recursion… let's call it _z
ertes 2017-01-29 09:23:43
prt [] = _z -- in this case _z = pure ()
ertes 2017-01-29 09:23:55
go [] ys = ys -- in this case _z = ys
ertes 2017-01-29 09:24:04
whoops
ertes 2017-01-29 09:24:07
go [] ys = _z -- in this case _z = ys
ph88 2017-01-29 09:24:32
don't see how ys = pure () being the same base
ertes 2017-01-29 09:24:46
this is just a symbolic game
ph88 2017-01-29 09:24:50
ah ok
ertes 2017-01-29 09:24:58
i've renamed the result for the empty case to _z
ertes 2017-01-29 09:25:02
good so far?
ph88 2017-01-29 09:25:10
ok
ertes 2017-01-29 09:25:25
less obviously both do something with the head element and the recursive result
ertes 2017-01-29 09:26:00
go (x : xs) ys = x : (xs ++ ys)
ertes 2017-01-29 09:26:46
prt (x : xs) = putStrLn x >> prt xs
ertes 2017-01-29 09:27:32
it may be less obvious how these two are the same pattern, but here is the secret: for both cases there is a function _f that takes the head element as well as the recursive result as its arguments
ertes 2017-01-29 09:27:43
go (x : xs) ys = _f x (xs ++ ys)
ertes 2017-01-29 09:27:46
what's _f in this case?
ph88 2017-01-29 09:28:11
:
Zemyla 2017-01-29 09:28:25
Okay, question. If something has a StablePtr made of it, it doesn't get GCed anymore until the StablePtr is released, right?
ertes 2017-01-29 09:28:30
yeah (nitpick: it's (:))
ertes 2017-01-29 09:28:48
ph88: prt (x : xs) = _f x (prt xs) -- what about this one?
ph88 2017-01-29 09:28:56
>>
ertes 2017-01-29 09:29:00
ph88: nope
ph88 2017-01-29 09:29:43
\a b -> putStrln a >> b
akfp 2017-01-29 09:29:46
How do I write a function that calculates the average of two numbers and that operate on both Integral and Fractional?
ertes 2017-01-29 09:29:53
ph88: exactly
akfp 2017-01-29 09:29:53
returning Double
ertes 2017-01-29 09:30:21
ph88: everything else about these two functions is the same… they only differ in _f and _z
ph88 2017-01-29 09:30:33
yup
ertes 2017-01-29 09:31:00
ph88: factoring out means: write a function that abstracts over _f and _z: reduce f z (x : xs) = f x (reduce f z xs); reduce f z [] = z
ph88 2017-01-29 09:31:02
nitpick: \a b -> putStrLn a >> b
ertes 2017-01-29 09:31:21
hehe yeah
ertes 2017-01-29 09:31:50
ph88: then you can write: go xs ys = reduce (:) ys xs
ph88 2017-01-29 09:31:57
ah yes
ertes 2017-01-29 09:32:19
ph88: and: prt = reduce (\x go -> putStrLn x >> go) (pure ())
ertes 2017-01-29 09:33:11
ph88: this kind of pattern abstraction is at the heart of functional programming =)
ph88 2017-01-29 09:33:19
and now we implemented foldl with reduce or what
ertes 2017-01-29 09:33:25
ph88: foldr
ph88 2017-01-29 09:34:16
foldr ? isn't that suppose to start from the right side of the list ?
ertes 2017-01-29 09:34:17
akfp: Integral has toInteger
ertes 2017-01-29 09:34:22
ph88: nope
ph88 2017-01-29 09:34:55
oh i always thought foldr stood for fold right
c_wraith 2017-01-29 09:35:02
it does
ertes 2017-01-29 09:35:05
ph88: right-associative fold
c_wraith 2017-01-29 09:35:08
But that doesn't mean "start from the right"
ph88 2017-01-29 09:35:23
omg -___-
ph88 2017-01-29 09:35:33
okay
c_wraith 2017-01-29 09:35:41
starting from the right would be *terrible* for performance.
ph88 2017-01-29 09:36:39
yeah i always wondered why foldl was slower than foldr, the opposite seemed to make more sense
ertes 2017-01-29 09:36:43
ph88: the second step to mastering this abstraction technique is to "re-see" those patterns in new code and then use (in this case) foldr correctly
ph88 2017-01-29 09:37:13
ok but how is this gonna help finding groups in my grammar rules graph ?
ertes 2017-01-29 09:37:50
ph88: you need to find similar code patterns and figure out over what you need to abstract, similar to 'go' and 'prt'
ph88 2017-01-29 09:39:02
well i factored out some code already, but the problem is that i need to make the rules so that all dependencies are there otherwise i get a lot of errors by ghc
ph88 2017-01-29 09:40:19
for example data HigherRule = HR LowerRule data LowerRule = LR String
ertes 2017-01-29 09:40:48
those two aren't mutual… they can easily live in separate modules
ertes 2017-01-29 09:41:11
however, it's more common to have a .Types module that has only types and instances
ph88 2017-01-29 09:41:26
yeah just that there are over 150 rules and i have little insight into the dependencies they have on each other .. this was just a simple example
ph88 2017-01-29 09:43:55
i will start with the rules that don't have any dependencies, check if it compiles and go from there i guess ^^
ph88 2017-01-29 09:44:59
actually i'm thinking about writing a script that parses my source file and figure out in which order i should introduce the rules in a bottom-up way