Home freenode/#haskell: Logs Calendar

Logs: freenode/#haskell

←Prev  Next→ 502,152 events total
2021-04-13 13:11:48 <ski> (looking)
2021-04-13 13:12:12 × gzj quits (~gzj@unaffiliated/gzj) (Remote host closed the connection)
2021-04-13 13:12:30 × ddellacosta quits (~ddellacos@ool-44c73afa.dyn.optonline.net) (Read error: Connection reset by peer)
2021-04-13 13:12:32 gzj joins (~gzj@unaffiliated/gzj)
2021-04-13 13:12:42 ddellac__ joins (~ddellacos@ool-44c73afa.dyn.optonline.net)
2021-04-13 13:12:56 <ski> your bracketting is off. e.g.
2021-04-13 13:12:58 <ski> eval x count = (pow x count)/(factorial count) + (eval x count - 1)
2021-04-13 13:13:00 <ski> ought to be
2021-04-13 13:13:11 × gzj quits (~gzj@unaffiliated/gzj) (Remote host closed the connection)
2021-04-13 13:13:13 × ddellac__ quits (~ddellacos@ool-44c73afa.dyn.optonline.net) (Remote host closed the connection)
2021-04-13 13:13:14 <ski> eval x count = pow x count / factorial count + eval x (count - 1)
2021-04-13 13:13:31 gzj joins (~gzj@unaffiliated/gzj)
2021-04-13 13:13:37 <ski> (and the same problem, in a few other places ..)
2021-04-13 13:13:48 son0p joins (~ff@181.136.122.143)
2021-04-13 13:13:48 <ski> next thing, which is the actual type error you're getting
2021-04-13 13:14:06 <ski> `factorial count' is an `Integer', so you can't divide by it, using `/'
2021-04-13 13:14:30 <ski> try first converting it to a `Double', with `fromIntegral'/`fromInteger'
2021-04-13 13:14:37 <Komrad_Kafuka> is there a way to typecast stuff in haskell ?
2021-04-13 13:14:41 × rom1504 quits (rom1504@rom1504.fr) (Ping timeout: 250 seconds)
2021-04-13 13:15:07 <ski> no
2021-04-13 13:15:31 <ski> there's no casting (as a language construct), and no implicit coercion either
2021-04-13 13:15:54 <ski> (it doesn't play along well with type inference)
2021-04-13 13:16:38 <Komrad_Kafuka> after that I am getting another error https://paste.tomsmeding.com/b1RYmqe6
2021-04-13 13:16:44 <Komrad_Kafuka> could you help me with this too
2021-04-13 13:16:54 <ski> instead of `forM_ [1 .. n] $ \_ -> do ..', you can use `replicateM_ n $ do ..'
2021-04-13 13:17:03 <ski> @type replicateM_
2021-04-13 13:17:05 <lambdabot> Applicative m => Int -> m a -> m ()
2021-04-13 13:17:13 × gzj quits (~gzj@unaffiliated/gzj) (Read error: Connection reset by peer)
2021-04-13 13:17:24 jamm_ joins (~jamm@unaffiliated/jamm)
2021-04-13 13:17:33 gzj joins (~gzj@unaffiliated/gzj)
2021-04-13 13:17:43 <ski> and then there'd be no need for the `:: IO Int' type ascription on `readLn' (since the type of `n' would be inferred from passing it to `replicateM_')
2021-04-13 13:18:09 × gzj quits (~gzj@unaffiliated/gzj) (Remote host closed the connection)
2021-04-13 13:18:17 <Komrad_Kafuka> nope showing me the same error
2021-04-13 13:18:24 <ski> similarly, since `eval' is declared to take a `Double', the type ascription on the other `readLn' is also redundant
2021-04-13 13:18:30 gzj joins (~gzj@unaffiliated/gzj)
2021-04-13 13:19:03 <ski> (you could still keep them, if you wanted to, if you think it makes the code more readable to you. however, i'd prefer using x :: Double <- readLn' in that case .. although that syntax requires a language extension)
2021-04-13 13:19:11 × gzj quits (~gzj@unaffiliated/gzj) (Remote host closed the connection)
2021-04-13 13:19:15 rj joins (~x@gateway/tor-sasl/rj)
2021-04-13 13:19:31 gzj joins (~gzj@unaffiliated/gzj)
2021-04-13 13:19:40 <ski> Komrad_Kafuka : yes, as i said, because you have the same problem (bracketting) in a few other places as well .. and you haven't fixed them yet
2021-04-13 13:20:07 <Komrad_Kafuka> oh ok I'll do them
2021-04-13 13:20:42 <ski> first you should make sure you understand my removal and addition of brackets, to the line i showed
2021-04-13 13:20:56 <Komrad_Kafuka> nope I don't sorry
2021-04-13 13:20:58 <ski> (feel free to ask more, if you're unsure)
2021-04-13 13:21:10 × gzj quits (~gzj@unaffiliated/gzj) (Remote host closed the connection)
2021-04-13 13:21:13 <ski> (pow x count)/(factorial count)
2021-04-13 13:21:17 <ski> already means the same thing as
2021-04-13 13:21:22 <ski> pow x count / factorial count
2021-04-13 13:21:31 gzj joins (~gzj@unaffiliated/gzj)
2021-04-13 13:21:37 <ski> since "function application binds tighter (has higher precedence than) any operator"
2021-04-13 13:21:46 <Komrad_Kafuka> but that does not break the code does it ?
2021-04-13 13:21:47 <ski> which is why those brackets were redundant
2021-04-13 13:21:49 <ski> secondly
2021-04-13 13:22:01 <ski> eval x count - 1
2021-04-13 13:22:03 <ski> actually means
2021-04-13 13:22:06 <ski> (eval x count) - 1
2021-04-13 13:22:10 <ski> which is not what you intended
2021-04-13 13:22:16 <ski> presumably you meant
2021-04-13 13:22:21 <ski> eval x (count - 1)
2021-04-13 13:22:25 <ski> and you'll have to spell that out
2021-04-13 13:22:42 <Komrad_Kafuka> oh , yeah I changed that actually it should have been eval x (count+1) since it has an upper bound
2021-04-13 13:22:45 × Unhammer quits (~Unhammer@gateway/tor-sasl/unhammer) (Ping timeout: 240 seconds)
2021-04-13 13:22:48 <ski> Komrad_Kafuka : correct. the first (redundant) part, didn't break the code
2021-04-13 13:23:07 × gzj quits (~gzj@unaffiliated/gzj) (Read error: Connection reset by peer)
2021-04-13 13:23:18 × frozenErebus quits (~frozenEre@37.231.244.249) (Ping timeout: 240 seconds)
2021-04-13 13:23:27 gzj joins (~gzj@unaffiliated/gzj)
2021-04-13 13:23:44 <ski> you might want to make `eval' give an error, or maybe terminate, in case the input is larger than the upper bound ?
2021-04-13 13:24:08 × gzj quits (~gzj@unaffiliated/gzj) (Read error: Connection reset by peer)
2021-04-13 13:24:12 <ski> (you could also pass in the upper bound as a separate argument, to make `eval' less ad hoc (less magic numbers))
2021-04-13 13:25:40 <Komrad_Kafuka> It's a small hackerrank problem so I don;t think I have to make it robust, I did the changes and here is the code but it's still giving me an error
2021-04-13 13:25:45 <Komrad_Kafuka> https://paste.tomsmeding.com/CLJfI7E6
2021-04-13 13:27:23 <ski> oh, actually, i see your `count' is an accumulator (an index variable, a counter, in this case), intended to be started / initialized, at zero. in such case, it's better to define a "wrapper" function, whose sole purpose is to initialize this parameter for you (and then your `main' should call this, not the current `eval'). usually, it then makes sense to define the "worker" (your `eval') inside a `where'
2021-04-13 13:27:29 <ski> (or `let'-`in') inside the wrapper (unless the worker has independent interest)
2021-04-13 13:28:02 cr3 joins (~cr3@192-222-143-195.qc.cable.ebox.net)
2021-04-13 13:28:27 <ski> "I don;t think I have to make it robust" -- it's up to you, of course. just saying it could be good practice. but the most important thing is that you're aware of it, and make a conscious choice
2021-04-13 13:28:57 <ski> again, you're not using brackets properly
2021-04-13 13:28:59 × acidjnk_new quits (~acidjnk@p200300d0c72b9561cdce4d77de6ce0bc.dip0.t-ipconnect.de) (Ping timeout: 250 seconds)
2021-04-13 13:29:29 <ski> (unlike in some other programming languages) brackets are not used for "function calls" in Haskell. however, they *are* used for grouping, and that's how you need to use them here !
2021-04-13 13:29:46 <ski> (exactly like how they're used for grouping, in `eval x (count - 1)')
2021-04-13 13:30:28 <Komrad_Kafuka> yeah I did hear something about auxilary functions in one video, I might reimplement it with your suggested changes but the thing is I still don't know what the error is, I did correct the brackets so one error down and one error still persists which is this https://paste.tomsmeding.com/nB6RwfnJ
2021-04-13 13:30:31 × OneFixt quits (~OneFixt@217.146.82.202) (Remote host closed the connection)
2021-04-13 13:30:45 <ski> ok
2021-04-13 13:30:57 <ski> consider e.g.
2021-04-13 13:31:01 <ski> pow x count
2021-04-13 13:31:07 <ski> this actually means
2021-04-13 13:31:11 <ski> (pow x) count
2021-04-13 13:31:18 ddellacosta joins (~ddellacos@ool-44c73afa.dyn.optonline.net)
2021-04-13 13:31:59 <ski> because when you define, and call, a function in this style (called "curried style"), you're actually defining a function that takes an input, and then returns as result another function, that takes another input parameter, before giving back the "final answer"
2021-04-13 13:32:10 <ski> so
2021-04-13 13:32:12 <ski> pow :: Double -> Integer -> Double
2021-04-13 13:32:14 <ski> actually means
2021-04-13 13:32:17 <ski> pow :: Double -> (Integer -> Double)
2021-04-13 13:33:38 Major_Biscuit joins (~Major_Bis@wlan-145-94-219-47.wlan.tudelft.nl)
2021-04-13 13:34:12 <ski> (all functions in Haskell take exactly one input argument/parameter. however, colloquially, we speak of "multi-parameter functions". but those are really only in our heads. one way of simulating them in Haskell is as the above, "curried style". another is "tupled style", which would be called like `pow (x,count)', defined like `pow :: (Double,Integer) -> Double', `pow (_,0) = 1', `pow (0,_) = 1', `pow (x,n)
2021-04-13 13:34:18 <ski> = ..x..n..')
2021-04-13 13:34:25 <Komrad_Kafuka> Oh, yeah I corrected that rn and now there's a whole list of other errors, correcting them one by one would be tedious, I think if I see your code on how to implement this that would be great, here's the problem that I wanted to solve https://www.hackerrank.com/challenges/eval-ex/problem?h_r=next-challenge&h_v=zen&isFullScreen=true
2021-04-13 13:34:36 <ski> because of this (and because `print' is just an ordinary library function), when you write
2021-04-13 13:34:39 <ski> print eval x 0
2021-04-13 13:34:41 <ski> this actually means
2021-04-13 13:34:46 <ski> ((print eval) x) 0

All times are in UTC.