dmp450 2017-01-31 08:45:25
hey all, I have a question. I'm doing a homework assignment and I'm new to Haskell. My assignment has me check for whether a number is even or odd using if-else, guards, and pattern matching.
dmp450 2017-01-31 08:45:51
I'm unable to figure out how to do the pattern matching bit. Is anyone able to give a hint on this?
dmp450 2017-01-31 08:46:33
I know I can't use `mod` on the left hand side, I can't use ==, I couldn't find a way to just check the last digit
tdammers 2017-01-31 08:47:07
you can use `mod`, you just can't use it as a pattern to match on
kadoban 2017-01-31 08:47:33
dmp450: You can pattern match on 0 and then use that to give answers for everything else. It's not really a *good* idea per se, but it should work. It's not quite clear to me if that's what you're supposed to do though.
tdammers 2017-01-31 08:47:36
but something like case number `mod` 2 of { 0 -> "it's even!"; 1 -> "this is odd" }
tdammers 2017-01-31 08:47:51
would work
tdammers 2017-01-31 08:48:16
remember that case introduces a pattern matching context
dmp450 2017-01-31 08:48:48
kadoban: what do you mean by pattern match on 0?
kadoban 2017-01-31 08:49:12
even 0 = something
ongy 2017-01-31 08:50:25
I wonder how ghc would react if I had a function that matches on every possible Int (let's say Int16, to not explode my storage)
kadoban 2017-01-31 08:50:59
I can only imagine poorly xD
dmp450 2017-01-31 08:51:06
tdammers: I don't think we've been intoduced to case yet
tdammers 2017-01-31 08:51:25
dmp450: then you'll need a helper function
tdammers 2017-01-31 08:52:08
or you could have 3 patterns to match against: 0, 1, and anything else
tdammers 2017-01-31 08:52:22
the "anything else" case would do the `mod` and recurse into the 0 or 1 case
tdammers 2017-01-31 08:52:29
isEven 0 = True
tdammers 2017-01-31 08:52:33
isEven 1 = False
tdammers 2017-01-31 08:52:42
isEven n = isEven (n `mod` 2)
dmp450 2017-01-31 08:52:42
tdammers: I was thinking of using a helper function, but couldn't think of how
tdammers 2017-01-31 08:52:54
with the helper:
dmp450 2017-01-31 08:52:56
oh, interesting method
tdammers 2017-01-31 08:52:58
helper 0 = True
tdammers 2017-01-31 08:53:02
helper 1 = False
tdammers 2017-01-31 08:53:09
isEven n = helper (n `mod` 2)
tdammers 2017-01-31 08:53:33
but now that I think about it, there isn't really a reason to have the helper at all
tdammers 2017-01-31 08:53:46
plus the helper is partial, which is kind of a smell
dmp450 2017-01-31 08:54:10
ultimately what I'm doing is taking two inputs, n and k, and I raise n to the power of k, but in two different ways depending if k is even or odd
dmp450 2017-01-31 08:54:38
https://bpaste.net/show/f0a20aea9b84
orzo 2017-01-31 08:54:58
is there a run-time cost for using 'or' with a list rather than || for mere stylistic reasons? Particularly if there is only two conditions to combine?
kadoban 2017-01-31 08:58:20
Seems like potentially sure. I have no idea if it can be optimized away ... kind of doubt it. But it should be a tiny difference.
Unode 2017-01-31 08:59:07
Is there any way have stack with "docker: true" work globally? I.e. some way to avoid "Cannot determine project root directory for Docker sandbox." when running stack ghci; or any "stack exec" from outside a project dir?
orzo 2017-01-31 09:15:10
it's odd that deriving Ord doesn't also derive Eq
orzo 2017-01-31 09:15:40
shouldn't Eq be a required parent class for Ord?
tsahyt 2017-01-31 09:18:52
the call to or doesn't seem to get inlined in my tiny test
dolio 2017-01-31 09:19:48
It is required. But just because you derive Ord doesn't mean you derive Eq.
ertes 2017-01-31 09:20:03
helo
orzo 2017-01-31 09:25:15
well the error I got wasn't on the deriving line telling me i was missing Eq. It was when i tried to use Eq somewhere, giving me the impression that if i used Ord instead, all would be fine.
dolio 2017-01-31 09:30:05
If you derive Ord without somehow implementing Eq, you will get an error saying that you're not allowed to derive Ord without implementing Eq.
dolio 2017-01-31 09:30:41
I don't know if it will always be the first error you get, or if some other error won't cause it to not occur.
dolio 2017-01-31 09:31:03
But it will be an error eventually if you don't fix it.
ClaudiusMaximus 2017-01-31 09:39:14
ugh, installing ghc-8.0.2 has broken my ghc-8.0.1 which now errors like ../lib/ghc-8.0.2/package.conf.d/package.cache: GHC.PackageDb.readPackageDb: inappropriate type (not enough bytes)
ClaudiusMaximus 2017-01-31 09:39:41
was hoping not to have to reinstall my sandbox tonight
ClaudiusMaximus 2017-01-31 09:40:14
maybe it's a problem with cabal