Search Haskell Channel Logs

Tuesday, March 7, 2017

#haskell channel featuring ski, jan_path, tdammers, lelf, lambdabot, Robert__,

Robert__ 2017-03-07 01:08:58
ok let's say I want the square numbers less than 50, so myPredicate needs to check if the input is a perfect square.
Robert__ 2017-03-07 01:09:14
result = takeWhile (square) [1..50]
Robert__ 2017-03-07 01:09:38
then how do I define 'square' using the 'where' notation?
Rodenbach 2017-03-07 01:11:14
I have data Foo c = Clear c | XYZ and want to match this bar when Clear wraps an Integer. bar (Clear (Integer i)) 20 = i+100 <= this doesn't work. How can I write this pattern?
ski 2017-03-07 01:12:00
Rodenbach : what is the expected type of `bar' ?
Rodenbach 2017-03-07 01:12:40
I want it to return whatever is wrapped by Clear.
ski 2017-03-07 01:13:02
Robert__ : `.. where square n = ..n..', where `..n..' is a boolean condition checking whether `n' is a square ?
tdammers 2017-03-07 01:13:17
Rodenbach: Integer is the name of the type, it's not a term-level constructor
ski 2017-03-07 01:13:29
Rodenbach : how about `bar :: Foo Integer -> Integer -> Integer' ?
ski 2017-03-07 01:13:51
Rodenbach : then `bar (Clear i) 20 = i+100' would work as a definition
Rodenbach 2017-03-07 01:13:55
tdammers: I also tried bar (Clear (i::Integer)) 10 = i+100
tdammers 2017-03-07 01:14:11
Rodenbach: and what is the error you get?
lelf 2017-03-07 01:14:20
[trifecta/parsers] Why on earth? runUnspaced <$> parseString (Unspaced <$> symbol "x" <* eof) mempty "x " => Success "x"
Rodenbach 2017-03-07 01:14:42
tdammers: Illegal type signature 'Integer'.
ski 2017-03-07 01:14:55
Rodenbach : what is your type signature ?
ski 2017-03-07 01:15:31
to use `i :: Integer' in a pattern like that you need (`PatternSignatures' or) `ScopedTypeVariables', a language extension, enabled
tdammers 2017-03-07 01:15:31
Rodenbach: try attaching a type signature to the entire function, rather than inside the pattern match
lyxia 2017-03-07 01:15:35
Rodenbach: you can't match on types if that's what you're tyring to do
Rodenbach 2017-03-07 01:15:52
lyxia: okay, I think I was trying that.
Rodenbach 2017-03-07 01:15:59
Will think about this then again, thanks.
tdammers 2017-03-07 01:16:18
Rodenbach: what lyxia said; you cannot dispatch on types at runtime, because types are resolved at compile time
ski 2017-03-07 01:16:24
you can't make `bar' behave one way if `i' is an `Integer', and another way if it isn't ..
lyxia 2017-03-07 01:16:38
Rodenbach: you can restrict the type of the function so that the caller can't ever pass something that doesn't wrap an integer.
ski 2017-03-07 01:16:46
.. without resorting to `Typeable, i.e. -- but you probably don't want to reach for that here
Robert__ 2017-03-07 01:17:07
:ski yes that makes sense
ski 2017-03-07 01:17:38
Robert__ : also, `takeWhile square [1 .. 50]' will work just fine, without the round brackets
lambdabot 2017-03-07 01:21:13
[0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225]
ski 2017-03-07 01:21:20
Robert__ : doesn't work for non-squares
ski 2017-03-07 01:21:36
Robert__ : try using `takeWhile' and `null', perhaps ?
lyxia 2017-03-07 01:22:31
jan_path: OH. I see sorry.
Robert__ 2017-03-07 01:22:36
what do you mean by 'null'
Robert__ 2017-03-07 01:22:40
looking it up on hoogle.
ski 2017-03-07 01:22:42
@src null
lambdabot 2017-03-07 01:22:42
null [] = True
lambdabot 2017-03-07 01:22:42
null (_:_) = False
lyxia 2017-03-07 01:22:43
lelf: you flipped the order of runSpaced and parseString I think.
Robert__ 2017-03-07 01:23:14
man that's useful
Robert__ 2017-03-07 01:23:27
I don't understand how I'd use null in this case
ski 2017-03-07 01:23:39
usually pattern-matching is nicer. sometimes `null' is handy
Robert__ 2017-03-07 01:23:59
right. Well, thanks for your help m8, I learnt something new about haskell today
ski 2017-03-07 01:24:00
> dropWhile (< 16) (map (^ 2) [0 ..])
lambdabot 2017-03-07 01:24:04
[16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484,529,5...
ski 2017-03-07 01:24:36
in the first case, the list starts with `16', in the second case it doesn't start with `7'
jan_path 2017-03-07 01:24:37
lelf: runUnspaced (symbol "x" *> eof :: Unspaced Parser String)
Robert__ 2017-03-07 01:25:13
> takeWhile (<50) $ map (^2) [1..]
ski 2017-03-07 01:25:17
> (takeWhile (<= 16) . dropWhile (< 16) . map (^ 2)) [0 ..]
lambdabot 2017-03-07 01:25:19
mueval-core: Time limit exceeded
lambdabot 2017-03-07 01:25:22
[16]
jan_path 2017-03-07 01:25:23
The signature should be `Unspaced Parser ()`
ski 2017-03-07 01:25:24
> (takeWhile (<= 7) . dropWhile (< 7) . map (^ 2)) [0 ..]
lambdabot 2017-03-07 01:25:29
[]
ski 2017-03-07 01:25:38
Robert__ : then you could use `null' on this list ..
Robert__ 2017-03-07 01:25:53
ah I understand
ski 2017-03-07 01:26:07
Robert__ : instead of doing a `takeWhile' after the `dropWhile', it might be simpler to just check the first element of the list directly ..
Robert__ 2017-03-07 01:26:30
that seems to make sense.
ski 2017-03-07 01:26:35
using `null' was the first thing that came to my mind, re `map (^ 2) [0 ..]'
jan_path 2017-03-07 01:26:52
lelf: Wrapping in Unspaced after using symbol does not do anything. You have to use the symbol of the `Unspaced Parser a' instance.
ski 2017-03-07 01:26:59
(note that i started at `0', not `1', because `0' is also a square)
lelf 2017-03-07 01:29:51
jan_path: ah! thanks!