Home freenode/#haskell: Logs Calendar

Logs: freenode/#haskell

←Prev  Next→ 502,152 events total
2021-05-10 18:12:25 <Andriamanitra> i understand that if i have a function that returns "Maybe a" it's easy to pattern match but what if i only had access to function like read that just causes an exception? is there a way to make a function that wraps that exception and returns "Maybe a" instead?
2021-05-10 18:12:48 <geekosaur> there is but it's in a separate package and it's fugly
2021-05-10 18:12:58 <monochrom> Demand access to a function that gives you Maybe instead.
2021-05-10 18:12:59 royal_screwup21 joins (52254809@gateway/web/cgi-irc/kiwiirc.com/ip.82.37.72.9)
2021-05-10 18:13:15 <geekosaur> Text.Read.readMaybe is the right solution here
2021-05-10 18:13:21 jgt_ joins (~jgt@92-247-237-116.spectrumnet.bg)
2021-05-10 18:13:38 khuldraeseth joins (~khuldraes@2600:6c48:647f:ea88:8c81:7883:e466:137b)
2021-05-10 18:14:12 <monochrom> What is the real question?
2021-05-10 18:14:21 × rj quits (~x@gateway/tor-sasl/rj) (Ping timeout: 240 seconds)
2021-05-10 18:14:26 enikar likes the couple readMaybe/fromMaybe
2021-05-10 18:14:39 <Andriamanitra> i'm just trying to understand the exception system and how to use it
2021-05-10 18:14:51 <Andriamanitra> but it seems it's better to avoid it altogether?
2021-05-10 18:15:05 <Iceland_jack> Andriamanitra: You can reify an exception with 'try' (https://hackage.haskell.org/package/base-4.15.0.0/docs/Control-Exception.html#v:try)
2021-05-10 18:15:06 <monochrom> But parsing is a wrong example for learning the exception system.
2021-05-10 18:15:19 <monochrom> <monochrom> Haskell's exception system works much better for I/O, but parsing is not I/O.
2021-05-10 18:15:29 <enikar> the exception system is usefull when working in IO, for reading/writing files, for example.
2021-05-10 18:16:02 <ski> Andriamanitra : you could also use `reads' directly (which is exported by `Prelude'), but it's more convenient to use `readMaybe', if you don't intend to continue parsing any trailing remainder
2021-05-10 18:16:07 <Andriamanitra> i see, i think i got confused because read by default uses errors.. you'd expect standard library to follow conventions
2021-05-10 18:16:23 × geekosaur quits (930099da@rrcs-147-0-153-218.central.biz.rr.com) (Quit: Connection closed)
2021-05-10 18:16:33 <ski> Andriamanitra : is there a reason why you want to use `error' for possibly user input ?
2021-05-10 18:16:52 geekosaur joins (930099da@rrcs-147-0-153-218.central.biz.rr.com)
2021-05-10 18:16:53 <monochrom> The standard library follows multiple conventions.
2021-05-10 18:17:07 merijn joins (~merijn@83-160-49-249.ip.xs4all.nl)
2021-05-10 18:17:28 <monochrom> Generally, any evolved-by-committee thing does.
2021-05-10 18:17:36 <Andriamanitra> ski: probably not but i'm doing an assignment and the spec says i should throw errors.. i guess the assignment is flawed
2021-05-10 18:17:39 × rajivr quits (uid269651@gateway/web/irccloud.com/x-jaamvvnjwwqairyx) (Quit: Connection closed for inactivity)
2021-05-10 18:17:41 × jgt_ quits (~jgt@92-247-237-116.spectrumnet.bg) (Ping timeout: 240 seconds)
2021-05-10 18:17:45 <enikar> I experiment a little exception in pure code, but they should only be catch in IO. And exception thrown in pure code are « imprecise ».
2021-05-10 18:18:07 <ski> Andriamanitra : does it require you to throw any particular error messages ?
2021-05-10 18:18:14 <Zemyla> You know, readMaybe doesn't necessarily protect against exceptions.
2021-05-10 18:18:15 <monochrom> Oh, for homework I would instruct students to throw errors and call it a day too.
2021-05-10 18:18:23 <Zemyla> > readMaybe "1 % 0" :: Maybe Rational
2021-05-10 18:18:25 <lambdabot> Just *Exception: Ratio has zero denominator
2021-05-10 18:18:33 kupi joins (uid212005@gateway/web/irccloud.com/x-ovwhwsshlhgdvfva)
2021-05-10 18:18:46 <monochrom> At least when the error case is beside the point of the homework in question.
2021-05-10 18:18:46 × dpl_ quits (~dpl@77-121-78-163.chn.volia.net) (Ping timeout: 240 seconds)
2021-05-10 18:19:08 <Andriamanitra> ski: yes, does that make a difference?
2021-05-10 18:19:30 <monochrom> What you will really learn in Haskell, at a meta level, is that we disbelieve in one-size-fits-all, unlike other cultures out there.
2021-05-10 18:19:52 <monochrom> If you're looking for "always use exception" or "never use exception" you will be disappointed.
2021-05-10 18:19:57 hackage simplexmq 0.3.1 - SimpleXMQ message broker https://hackage.haskell.org/package/simplexmq-0.3.1 (epoberezkin)
2021-05-10 18:20:18 idhugo joins (~idhugo@80-62-116-231-mobile.dk.customer.tdc.net)
2021-05-10 18:20:27 <ski> Andriamanitra : if you're okay with any error aborting the program, if the user input is invalid, you could use `readIO str' (this has the advantage that it will give the error at the point this is executed, rather than later, when you try to look at the animal)
2021-05-10 18:20:45 <jil> Ok, Is the same syntax as if I were in ghci :load myfile.hs
2021-05-10 18:20:47 <Iceland_jack> Haskell's philosophy has evolved, we care about totality a lot more now
2021-05-10 18:20:51 × star_cloud quits (~star_clou@ec2-34-220-44-120.us-west-2.compute.amazonaws.com) (Ping timeout: 260 seconds)
2021-05-10 18:21:08 <Zemyla> Which is why my read rational example is so galling to me.
2021-05-10 18:21:47 <Zemyla> I feel like read "1 % 0" should either return 1 :% 0 or fail to parse.
2021-05-10 18:22:03 rj joins (~x@gateway/tor-sasl/rj)
2021-05-10 18:23:00 <enikar> instead, it doesn't fail to parse. It fails when evaluating `1 % 0`
2021-05-10 18:24:25 <Zemyla> See, that's the big problem. If it's going to return an error, it should fail to parse instead.
2021-05-10 18:24:33 × Aquazi quits (uid312403@gateway/web/irccloud.com/x-pkpihoadcsprmglz) (Quit: Connection closed for inactivity)
2021-05-10 18:24:37 × thongpv87 quits (~thongpv87@27.76.236.94) (Remote host closed the connection)
2021-05-10 18:24:40 <monochrom> That is going to be the next great debate.
2021-05-10 18:24:55 <Zemyla> It can test for zero, because it has an Integral constraint.
2021-05-10 18:26:23 Shuppiluliuma joins (~shuppilul@153.33.68.161)
2021-05-10 18:26:30 RusAlex joins (~Chel@unaffiliated/rusalex)
2021-05-10 18:28:12 waleee-cl joins (uid373333@gateway/web/irccloud.com/x-tgwozfrjgszjeomb)
2021-05-10 18:28:26 <ski> Andriamanitra : fwiw, the inclusion of `Other' seems dubious
2021-05-10 18:29:41 × royal_screwup21 quits (52254809@gateway/web/cgi-irc/kiwiirc.com/ip.82.37.72.9) (Quit: Connection closed)
2021-05-10 18:30:00 royal_screwup21 joins (52254809@gateway/web/cgi-irc/kiwiirc.com/ip.82.37.72.9)
2021-05-10 18:30:38 × gitgood quits (~gitgood@80-44-12-75.dynamic.dsl.as9105.com) (Remote host closed the connection)
2021-05-10 18:30:49 <Andriamanitra> ski: yeah i would agree, but that's just a minimal example of what i'm trying to do, not the actual assignment/code
2021-05-10 18:32:16 <ski> Andriamanitra : as pertains to `Read', generally i'd prefer `readMaybe' or `reads' (or `readsPrec') over `read', unless you feel confident that you're not going to pass invalid input to it, or, should that happen, you're okay with the computation aborting. however, even in this case, if applicable/reasonable, using `readIO' or `readLn' would usually be preferrable
2021-05-10 18:34:37 star_cloud joins (~star_clou@ec2-34-220-44-120.us-west-2.compute.amazonaws.com)
2021-05-10 18:34:41 × royal_screwup21 quits (52254809@gateway/web/cgi-irc/kiwiirc.com/ip.82.37.72.9) (Ping timeout: 240 seconds)
2021-05-10 18:36:37 ixlun joins (~user@109.249.184.235)
2021-05-10 18:36:54 <Andriamanitra> ski: ok thanks, i'll be using readMaybe instead from now on - although for this assignment i'm not allowed to import anything outside prelude so i ended up writing my own function to replace read altogether
2021-05-10 18:37:41 × RusAlex quits (~Chel@unaffiliated/rusalex) (Ping timeout: 240 seconds)
2021-05-10 18:38:07 × khuldraeseth quits (~khuldraes@2600:6c48:647f:ea88:8c81:7883:e466:137b) (Quit: Quit)
2021-05-10 18:39:10 frozenErebus joins (~frozenEre@37.231.244.249)
2021-05-10 18:39:29 × star_cloud quits (~star_clou@ec2-34-220-44-120.us-west-2.compute.amazonaws.com) (Remote host closed the connection)
2021-05-10 18:39:46 star_cloud joins (~star_clou@ec2-34-220-44-120.us-west-2.compute.amazonaws.com)
2021-05-10 18:39:48 RusAlex joins (~Chel@unaffiliated/rusalex)
2021-05-10 18:40:04 sphalerite joins (~sphalerit@NixOS/user/lheckemann)
2021-05-10 18:43:07 <ski> readMaybe s0 = case [x | (x,s1) <- reads s0,("","") <- lex s1]
2021-05-10 18:43:21 <ski> [x] -> Just x
2021-05-10 18:43:22 chimera joins (~chimera@168-182-134-95.pool.ukrtel.net)
2021-05-10 18:43:28 <ski> _ -> Nothing
2021-05-10 18:43:44 <ski> (er, add an ` of' at the end of the first line)
2021-05-10 18:44:13 × Jesin quits (~Jesin@pool-72-66-101-18.washdc.fios.verizon.net) (Ping timeout: 240 seconds)
2021-05-10 18:44:32 × ystael quits (~ystael@209.6.50.55) (Ping timeout: 246 seconds)
2021-05-10 18:45:15 <ski> Andriamanitra : ^ takes care of using `reads' for you, then checking that there's nothing (apart from whitespace) after what you parsed, also checking that you get a single definite parse result
2021-05-10 18:45:27 tromp joins (~tromp@dhcp-077-249-230-040.chello.nl)
2021-05-10 18:47:38 royal_screwup21 joins (52254809@gateway/web/cgi-irc/kiwiirc.com/ip.82.37.72.9)
2021-05-10 18:47:43 <Andriamanitra> ski: can you explain the `("","") <- lex s1` part?
2021-05-10 18:48:18 <Andriamanitra> looks like it's putting something into tuple of empty strings but that doesn't make sense
2021-05-10 18:48:26 <ski> > reads " 23 " :: [(Int,String)]
2021-05-10 18:48:28 <lambdabot> [(23," ")]
2021-05-10 18:48:36 <ski> > (reads :: ReadS Int) " 23 "
2021-05-10 18:48:37 <lambdabot> [(23," ")]
2021-05-10 18:48:57 jgt_ joins (~jgt@92-247-237-116.spectrumnet.bg)
2021-05-10 18:48:58 <ski> `reads' gives a list of pairs of parsed result, and remainder of input
2021-05-10 18:49:34 × star_cloud quits (~star_clou@ec2-34-220-44-120.us-west-2.compute.amazonaws.com) (Excess Flood)
2021-05-10 18:49:54 <ski> so `s1' will be any remainder of the input `s0', after having parsed some initial prefix of it into the value `x'
2021-05-10 18:50:23 star_cloud joins (~star_clou@ec2-34-220-44-120.us-west-2.compute.amazonaws.com)
2021-05-10 18:51:21 <ski> `lex' parses a single (Haskell) token from a string (also giving back remainder of input, just like `reads'). as a special case, if there is no more tokens (so it's only whitespace, including when the remainder is empty), `lex' will give back `("","")' ("empty token with empty remainder") as result
2021-05-10 18:51:31 × merijn quits (~merijn@83-160-49-249.ip.xs4all.nl) (Ping timeout: 268 seconds)
2021-05-10 18:51:53 <ski> so, we're using `("","") <- lex s1' here to check that there's nothing more in the remainder, except possibly whitespace
2021-05-10 18:52:18 <ski> > (reads :: ReadS Int) " 23 abc 7 "
2021-05-10 18:52:20 <lambdabot> [(23," abc 7 ")]

All times are in UTC.