nubBy is not working as expected - haskell

The function below should generate prime numbers however it does not for GHC 7.10.2. Is anybody else seeing this?
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
Prelude> import Data.List
Prelude Data.List> print . take 100 . nubBy (\x y -> x `rem` y == 0) $ [2..]
[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101]
The strange part is that it seems to work fine on this site:
rextester.com/LWZCQ71376

What changed between base-4.7.x and base-4.8.0.0 is the definition of elem_by which is what nubBy is defined in terms of.
In base-4.7 elem_by has this clause:
elem_by eq y (x:xs) = y `eq` x || elem_by eq y xs
and in base-4.8 it was changed to:
elem_by eq y (x:xs) = x `eq` y || elem_by eq y xs
The history of this change is documented in these TRAC issues:
2528
3280
7913
Note that the Haskell Report Prelude version of nubBy is:
nubBy eq (x:xs) = x : nubBy eq (filter (\ y -> not (eq x y)) xs)
which was at odds with the base-4.7 implementation, so that also explains the change.

The order of the arguments have been flipped, it seems, in the new base. EDIT: I called this a bug, but as another answer points out the old behavior was the incorrect order.
You can see the order has been flipped by observing:
> print . take 5 . nubBy (\x y -> trace (show (x,y)) $ x `rem` y == 0) $ [2..]
[2(2,3)
,3(3,4)
(2,4)
,4(4,5)
(3,5)
(2,5)
Certainly rem 2 4 does not equal zero (it equals 2), so it yields 4.
Notice you get the result you desire when you flip the argument order in the lambda:
> print . take 5 . nubBy (\x y -> trace (show (x,y)) $ y `rem` x == 0) $ [2..]
[2(2,3)
,3(3,4)
(2,4)
(3,5)
(2,5)
....
EDIT: Since discussion indicates the relation is supposed to be equality and operate regardless of the order (and I'm too lazy to look at the report right now) notice you can compare the arguments first and get stable behavior either way:
print . take 100 . nubBy (\x y -> rem (max x y) (min x y) == 0) $ [2..]

Related

for beginners | luhn algorithm for list of integers

I have already seen this solution:
doubleAndSum :: [Int] -> Int
doubleAndSum = fst . foldr (\i (acc, even) -> (acc + nextStep even i, not even)) (0,False)
where
nextStep even i
| even = (uncurry (+) . (`divMod` 10) . (*2)) i
| otherwise = i
myLuhn :: Int -> Bool
myLuhn = (0 ==) . (`mod` 10) . doubleAndSum . (map (read . (: ""))) . show
testCC :: [Bool]
testCC = map myLuhn [49927398716, 49927398717, 1234567812345678, 1234567812345670]
-- => [True,False,False,True]
However, I don't understand it because I am new to Haskell.
luhn :: [Int] -> Bool
luhn w x y z = (luhnDouble w + x + luhnDouble y + z) `mod` 10 == 0
luhnDouble :: Int -> Int
luhnDouble x | 2* x <= 9 = 2*x
| otherwise = (2*x)-9
I understand this simplified version of the algorithm for only four digits.
However, I don't know how to write a version of the algorithm for a list of digits of any length.
Honestly, the example is pretty arcane. It makes excessive use of point-free style, i.e. omitting explicit function arguments. That can sometimes make code nice and concise, but it can also make code rather cryptic.
Let's start with this here:
(uncurry (+) . (`divMod` 10) . (*2)) i
First, since you're just applying everything to the argument i, there's no real need for having a composition pipeline – you might as well write it
uncurry (+) $ (`divMod` 10) $ (*2) i
≡ uncurry (+) $ (`divMod` 10) $ i*2
≡ uncurry (+) $ (i*2)`divMod`10
≡ let (d,r) = (i*2)`divMod`10
in d+r
So, nextStep could be written
nextStep isEven i
| isEven = d+r
| otherwise = i
where (d,r) = (i*2)`divMod`10
(I avoid the variable name even, which is also the name of the standard function that checks whether a number is even!)
Alternatively, you could just invoke your luhnDouble function here, which actually computes the same thing, just in a more verbose way:
nextStep isEven i
| isEven = luhnDouble i
| otherwise = i
Then you have this fold. It basically does three things interlocked: 1. toggle between even and odd 2. apply nextStep to each list element, together with the even-ness 3. sum up the results.
I don't agree that it's a good idea to do all of that with a single fold†; much clearer to write it out:
doubleAndSum = sum
. map (\(isEven, i) -> nextStep isEven i) -- or `map (uncurry nextStep)`
. zip (cycle [False, True]) -- or `iterate not False`
. reverse
The reverse is needed just to align the False with the last element of the input list, instead of its head; this is a bit ugly but uncritical.
The combination of map and zip has a standard shortcut that does both in one step:
doubleAndSum = sum
. zipWith nextStep (cycle [False, True])
. reverse
As for myLuhn: this is IMO actually ok to write in the point-free style, but I'd break it out a bit. Specifically,
decimalDigits :: Int -> [Int]
decimalDigits = map (read . (: "")) . show
What (:"") does is, it puts single characters into singleton-strings. Could also be written read . pure.
Then,
myLuhn = (0 ==) . (`mod` 10) . doubleAndSum . decimalDigits
or
myLuhn x = doubleAndSum (decimalDigits x)`mod`10 == 0
†There could be a case made that a single traversal is good for performance, however if you think on that level then it should almost certainly not be a lazy right fold over a list, but rather a strict left fold over an unboxed vector. Anyway, GHC can often fuse separate fold-y operations into a single traversal.

Couldn't match expected type ‘[Integer]’ with actual type ‘Integer’

I am working on creating a list of all the even numbers in the Fibonacci series which are less than or equal to 4,000,000. In Haskell, I've defined the Fibonacci series as:
fibs = 1 : 2 : next fibs
where
next (a : t#(b:_)) = (a+b) : next t
and am using the following list comprehension to construct my set:
[ x | x <- take 50 fibs, x `mod` 2 == 0, last x <= 4*10^6 ]
However, GHC is throwing a Couldn't match expected type ‘[Integer]’ with actual type ‘Integer’ error.
I understand that the predicate, last x <= 4*10^6, is responsible for the error. Inspired by hammar's answer here, my initial reaction was to ensure that 4*10^6 was the right type, so I tried rephrasing the predicate as last x <= toInteger 4*10^6 to no avail; same error. I also thought that maybe I needed to specify 4*10^6 as a singleton (i.e. [4*10^6]), but no luck there either.
I'm struggling to understand what is exactly going on and how best to resolve the issue.
sum [ x | x <- take 50 fibs, x `mod` 2 == 0, last x <= 4*10^6 ]
take 50 fibs is a list of Integer ([Integer]), and x is an element of that list (thus an Integer). last is a function which takes a list...
GHCi> :t last
last :: [a] -> a
... but you are passing an Integer to it. You don't need last to filter the elements in the list comprehension; just use:
sum [ x | x <- take 50 fibs, x `mod` 2 == 0, x <= 4*10^6 ]
By the way, given that you know that the numbers in fibs always increase, you can write your expression as:
-- (<= 4*10^6) is shorthand for (\x -> x <= 4*10^6)
sum [ x | x <- takeWhile (<= 4*10^6) fibs, x `mod` 2 == 0 ]
-- Three equivalent alternatives:
(sum . takeWhile (<= 4*10^6)) [ x | x <- fibs, x `mod` 2 == 0 ]
(sum . takeWhile (<= 4*10^6) . filter (\x -> x `mod` 2 == 0)) fibs
(sum . takeWhile (<= 4*10^6) . filter ((== 0) . (`mod` 2))) fibs
That way you don't need the arbitrary limit of 50 elements.
fibs :: [Integer]
take 50 fibs :: [Integer]
x <- take 50 fibs
x :: Integer
last :: [a] -> a
last x :: ???
Your list comprehension will typecheck if you remove last. I think what you meant to write is something more like
[ x | x <- takeWhile (<= 4*10^6) $ take 50 fibs, x `mod` 2 == 0 ]
though.

Why in this line of haskell, $ cannot replace ()

The code is
prime (x:xs) = x:prime (filter (\y -> y `mod` x /=0) xs)
If I change it to
prime (x:xs) = x:prime $ filter (\y -> y `mod` x /=0) xs
There is a pattern matching error.
If I try to reproduce the error, it seems like GHC understands it like so:
(x : prime) $ filter (\y -> y `mod` x /= 0) xs
But you want it like that:
x : (prime $ filter (\y -> y `mod` x /= 0) xs)
So you just have to use bracket notation, I think.
First of all, it's a misconception that $ is a syntactic replacement for () — $ is a regular operator (which itself is a function that is simply infix by default); it's just the precedence rules that make $ convenient in many cases — but there's no syntactic magic behind it.
Now, ignoring the irrelevant bits, let's just concentrate on the part of the code that's causing the error:
x : prime (foo bar)
is the same as
x : (prime (foo bar))
because the : on the left has a lower precedence than the function application on the right.
However
x : prime $ foo bar
is the same as
(x : prime) $ (foo bar)
which is the same as
(x : prime) (foo bar)
because the : on the left has a higher precedence than the $ on the right.
So the error comes from the fact that you're trying to apply the result of the expression x : prime to the argument foo bar as if x : prime were a function, but it's not, hence the type error.
Solution? Either throw in some parentheses, or even better, don't use the $ at all in this case.
When you write:
x : prime $ filter foo xs
It is interpreted as
(x : prime) $ filter foo xs
due to (:) having a higher precedence than ($). See the fixity declarations section of this report for more.
It fails because a list of functions ((x:prime)) is not a function, and cannot be used as such.
If you want to make the function prettier, try this:
prime (x:xs) = x : prime (filter ((/= 0) . (`mod` x)) xs)
Others have already explained the problem. Here is one solution:
prime (x:xs) = (x:) $ prime $ filter (\y -> y `mod` x /=0) xs
That is, you can use an operator section to express the application of : to x and then apply the result as you like.

What is the Haskell way of solving this?

New to Haskell. I have the following code
fac :: Integer -> [Integer]
fac x = take 1 $ map (x `mod` ) $ reverse [2 .. xs]
where xs = floor $ sqrt $ fromIntegral x
I want to display the first value from [2 .. xs] such that x mod y == 0. But I can't use filter as that only would work off the output from mod. How do I do this?
You can use filter. Just compose (==0) and (x `mod`) for the first argument of filter.
If you are trying to do Project Euler Problem 3, this will be an incredibly painful way of solving it.
I would suggest the following:
Define a function for what you have in the where clause, i.e. isqrt = round . sqrt . fromIntegral
Define two functions isPrime and primeList that are mutually recursive for calculating primes and you should use isqrt in the definition of isPrime, although the use of the isqrt isn't actually necessary; I just find it easier to think about for writing this function. (there is a hint below for how to do this step)
Using primeList, define a recursive factorize function that outputs a list of primes (or something similar)
Factorize the number from the question and return the largest prime in the list
What I have suggested above is usually not that great for calculating very large primes quickly, but it should be adequate for this problem. This paper goes into more detail why: http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
For future number theory related PE problems, look into using this library: https://hackage.haskell.org/package/arithmoi-0.2.0.4
Other comments:
Instead of take 1, you should actually use head; it's slightly better.
Also, since the biggest number you will deal with here is 600851475143, you don't actually need the function to have type Integer -> [Integer]. 32-bit Int doesn't do the trick, because maxBound :: Int is 2147483647. maxBound :: Int64, however, is 9223372036854775807, which is much more than you need. So you could just define your function to have type Int64 -> [Int64] (or just Int -> [Int] if the machine is 64-bit already).
Hint:
primeList should be defined as something like primeList = 2 : 3 : filter isPrime [5,7..], but you can decide to be as fancy as you want with this); again, these definitions should be mutually recursive.
I want to display the first value from [2 .. xs] such that x mod y == 0
In this case, you should not use reverse:
fac x = take 1 $ filter (\y -> x `mod` y == 0) $ reverse [2 .. xs]
where xs = floor $ sqrt $ fromIntegral x
If you want to find out the first value starting from xs such that mod x y == 0, there is also no need to use reverse:
fac x = take 1 $ filter (\y -> x `mod` y == 0) $ [xs, xs-1 .. 2]
where xs = floor $ sqrt $ fromIntegral x
fac x = take 1 $ filter (\y -> x `mod` y == 0 ) $ reverse [2 .. xs]
where xs = floor $ sqrt $ fromIntegral x
or, to be point-free:
fac x = take 1 $ filter ((== 0).(x `mod`)) $ reverse [2 .. xs]
where xs = floor $ sqrt $ fromIntegral x
Not sure if you want reverse here, or what exactly is your goal.
You can use filter. Here's a one-liner (well, two-liner if you count the import):
import Data.Ix (range)
fac x = head . filter ((==) 0 . mod x) . curry range 2 . floor . sqrt . fromIntegral $ x

Haskell prime test

I'm new to Haskell, and I'm trying a bit:
isPrime :: Integer->Bool
isPrime x = ([] == [y | y<-[2..floor (sqrt x)], mod x y == 0])
I have a few questions.
Why when I try to load the .hs, WinHugs say: Instances of (Floating Integer, RealFrac Integer) required for definition of isPrime?
When the interpreter finds one element in the right set, it immediately stops or it computes all the set? I think you know what I mean.
Sorry about my english.
1) The problem is that sqrt has the type (Floating a) => a -> a, but you try to use an Integer as argument. So you have to convert your Integer first to a Floating, e.g. by writing sqrt (fromIntegral x)
2) I see no reason why == shouldn't be lazy, but for testing for an empty collection you can use the null function (which is definitely lazy, as it works on infinite lists):
isPrime :: Integer->Bool
isPrime x = null [y | y<-[2..floor (sqrt (fromIntegral x))], x `mod` y == 0]
But in order to get an more idiomatic solution, break the problem into smaller sub-problems. First, we need a list of all elements y with y*y <= x:
takeWhile (\y -> y*y <= x) [2..]
Then we need only the elements that divide x:
filter (\y -> x `mod`y == 0) (takeWhile (\y -> y*y <= x) [2..])
Then we need to check if that list is empty:
isPrime x = null (filter (\y -> x `mod`y == 0) (takeWhile (\y -> y*y <= x) [2..]))
And if this looks to lispy to you, replace some of the parens with $
isPrime x = null $ filter (\y -> x `mod` y == 0) $ takeWhile (\y -> y*y <= x) [2..]
For additional clarity you can "outsource" the lambdas:
isPrime x = null $ filter divisible $ takeWhile notTooBig [2..] where
divisible y = x `mod`y == 0
notTooBig y = y*y <= x
You can make it almost "human readable" by replacing null $ filter with not $ any:
isPrime x = not $ any divisible $ takeWhile notTooBig [2..] where
divisible y = x `mod`y == 0
notTooBig y = y*y <= x
Because sqrt has the type Floating a => a -> a. This means the input has to be a Floating type and the output will be the same type. In other words x needs to be a Floating type. However you declared x to be of type Integer, which is not a Floating type. In addition floor needs a RealFrac type, so x needs to be that as well.
The error message suggests that you fix that by making Integer a Floating type (by defining an instance Floating Integer (and the same for RealFrac).
Of course this is not the correct approach in this case. Rather you should use fromIntegral to convert x to a Real (which is an instance of Floating and RealFrac) and then give that to sqrt.
Yes. As soon as == sees that the right operand has at least one element, it knows it is not equal to [] and thus returns False.
That being said, null is a more idiomatic way to check whether a list is empty than [] ==.
Regarding the second point, it stops, for example:
[] == [x | x <- [1..]]
Returns False
Landei's solution is great, however, if you want a more efficient¹ implementation we have (thanks to BMeph):
-- list of all primes
primes :: [Integer]
primes = sieve (2 : 3 : possible [1..]) where
sieve (p : xs) = p : sieve [x | x <- xs, x `mod` p > 0]
possible (x:xs) = 6*x-1 : 6*x+1 : possible xs
isPrime :: Integer -> Bool
isPrime n = shortCircuit || (not $ any divisible $ takeWhile inRangeOf primes) where
shortCircuit = elem n [2,3] || (n < 25 && ((n-1) `mod` 6 == 0 || (n+1) `mod` 6 == 0))
divisible y = n `mod` y == 0
inRangeOf y = y * y <= n
The 'efficiency' comes from the use of constant primes. It improves the search in two ways:
The Haskell runtime could cache the results so subsequent invocations are not evaluated
It eliminates a range of numbers by logic
note that the sieve value is simply a recursive table, where says the head of
the list is prime, and adds it to it. For the rest of the lists if there is no
other value already in the list that composes the number then its also prime
possible is list of all possible primes, since all possible primes are in the
form 6*k-1 or 6*k-1 except 2 and 3
The same rule is applied for shortCircuit too to quickly bail out of calculations
Footnote by D.F.
¹ It's still a terribly inefficient way to find primes. Don't use trial division if you need primes larger than a few thousand, use a sieve instead. There are several far more efficient implementations on hackage.
I think WinHugs needs to import a module for Integer and etc... Try Int
The interpreter will not compute anything until you call e.g. isPrime 32 then it will lazily compute the expression.
PS your isPrime implementation is not the best implementation!

Resources