Inverted Sum of a Number -- Haskell - haskell

I have the following function in Haskell:
invertedSum :: Integer -> Integer
invertedSum n = n + (read . reverse . show) n
I need to know the number of sums that must be made in order for the number to be capicua. That is to say,
invertedSum 1999 = 11990 (1999+9991)
invertedSum 11990 = 21901
invertedSum 21901 = 32813
invertedSum 32813 = 64636
invertedSum 64636 = 128282
invertedSum 128282 = 411103
invertedSum 411103 = 712217
isCapicua :: Integer -> Bool
isCapicua n | show n == (reverse . show) n = True
| otherwise = False
isCapicua 712217 == True
So, I want to generate the following list, but I don't know how.
sumUpToCapicua 1999 = [11990, 21901, 32813, 64636, 128282, 411103, 712217]
genericLength (sumUpToCapicua 1000000079994144385) == 259

You already have a function invertedSum with the type Integer -> Integer. If I understand the question correctly, you'd like to apply it multiple times, starting with a particular Integer (e.g. 1999).
You could use iterate for that purpose. It has the type:
Prelude> :t iterate
iterate :: (a -> a) -> a -> [a]
In other words, it'll take any function a -> a, as well as an initial value a, and produce an infinite list of a values.
In your case, invertedSum has the type Integer -> Integer, and the initial value you'd like to use (e.g. 1999) would also be of the type Integer, so it all fits: a would be Integer.
Try using invertedSum and e.g. 1999 with iterate. Be aware that this produces an infinite list, so if you experiment with this in GHCi, you probably want to use e.g. take 10 to cap the number of values generated.

Related

Haskell: Difference between "(Int a, Bool b) => a -> b" and Int -> Bool [duplicate]

I wrote my first program in Haskell today. It compiles and runs successfully. And since it is not a typical "Hello World" program, it in fact does much more than that, so please congrats me :D
Anyway, I've few doubts regarding my code, and the syntax in Haskell.
Problem:
My program reads an integer N from the standard input and then, for each integer i in the range [1,N], it prints whether i is a prime number or not. Currently it doesn't check for input error. :-)
Solution: (also doubts/questions)
To solve the problem, I wrote this function to test primality of an integer:
is_prime :: Integer -> Bool
is_prime n = helper n 2
where
helper :: Integer -> Integer -> Bool
helper n i
| n < 2 * i = True
| mod n i > 0 = helper n (i+1)
| otherwise = False
It works great. But my doubt is that the first line is a result of many hit-and-trials, as what I read in this tutorial didn't work, and gave this error (I suppose this is an error, though it doesn't say so):
prime.hs:9:13:
Type constructor `Integer' used as a class
In the type signature for `is_prime':
is_prime :: Integer a => a -> Bool
According to the tutorial (which is a nicely-written tutorial, by the way), the first line should be: (the tutorial says (Integral a) => a -> String, so I thought (Integer a) => a -> Bool should work as well.)
is_prime :: (Integer a) => a -> Bool
which doesn't work, and gives the above posted error (?).
And why does it not work? What is the difference between this line (which doesn't work) and the line (which works)?
Also, what is the idiomatic way to loop through 1 to N? I'm not completely satisfied with the loop in my code. Please suggest improvements. Here is my code:
--read_int function
read_int :: IO Integer
read_int = do
line <- getLine
readIO line
--is_prime function
is_prime :: Integer -> Bool
is_prime n = helper n 2
where
helper :: Integer -> Integer -> Bool
helper n i
| n < 2 * i = True
| mod n i > 0 = helper n (i+1)
| otherwise = False
main = do
n <- read_int
dump 1 n
where
dump i x = do
putStrLn ( show (i) ++ " is a prime? " ++ show (is_prime i) )
if i >= x
then putStrLn ("")
else do
dump (i+1) x
You are misreading the tutorial. It would say the type signature should be
is_prime :: (Integral a) => a -> Bool
-- NOT Integer a
These are different types:
Integer -> Bool
This is a function that takes a value of type Integer and gives back a value of type Bool.
Integral a => a -> Bool
This is a function that takes a value of type a and gives back a value of type Bool.
What is a? It can be any type of the caller's choice that implements the Integral type class, such as Integer or Int.
(And the difference between Int and Integer? The latter can represent an integer of any magnitude, the former wraps around eventually, similar to ints in C/Java/etc.)
The idiomatic way to loop depends on what your loop does: it will either be a map, a fold, or a filter.
Your loop in main is a map, and because you're doing i/o in your loop, you need to use mapM_.
let dump i = putStrLn ( show (i) ++ " is a prime? " ++ show (is_prime i) )
in mapM_ dump [1..n]
Meanwhile, your loop in is_prime is a fold (specifically all in this case):
is_prime :: Integer -> Bool
is_prime n = all nondivisor [2 .. n `div` 2]
where
nondivisor :: Integer -> Bool
nondivisor i = mod n i > 0
(And on a minor point of style, it's conventional in Haskell to use names like isPrime instead of names like is_prime.)
Part 1: If you look at the tutorial again, you'll notice that it actually gives type signatures in the following forms:
isPrime :: Integer -> Bool
-- or
isPrime :: Integral a => a -> Bool
isPrime :: (Integral a) => a -> Bool -- equivalent
Here, Integer is the name of a concrete type (has an actual representation) and Integral is the name of a class of types. The Integer type is a member of the Integral class.
The constraint Integral a means that whatever type a happens to be, a has to be a member of the Integral class.
Part 2: There are plenty of ways to write such a function. Your recursive definition looks fine (although you might want to use n < i * i instead of n < 2 * i, since it's faster).
If you're learning Haskell, you'll probably want to try writing it using higher-order functions or list comprehensions. Something like:
module Main (main) where
import Control.Monad (forM_)
isPrime :: Integer -> Bool
isPrime n = all (\i -> (n `rem` i) /= 0) $ takeWhile (\i -> i^2 <= n) [2..]
main :: IO ()
main = do n <- readLn
forM_ [1..n] $ \i ->
putStrLn (show (i) ++ " is a prime? " ++ show (isPrime i))
It is Integral a, not Integer a. See http://www.haskell.org/haskellwiki/Converting_numbers.
map and friends is how you loop in Haskell. This is how I would re-write the loop:
main :: IO ()
main = do
n <- read_int
mapM_ tell_prime [1..n]
where tell_prime i = putStrLn (show i ++ " is a prime? " ++ show (is_prime i))

How to convert Integer in String and String in Integer in Haskell?

I'm still learning Haskell and I'm really confused with this language... i have to implement two functions fromInteger :: Integer -> String and toInteger :: String -> Integer that translate between Haskell integers and numbers by strings of digits in reverse order, like: "25" -> "52". For the sake of functional decomposition i should first implement fromDigit :: Integer -> Char and toDigit :: Char -> Integer
that translate between digits and characters.
How should the function look like?
Thanks a lot!
You can use read :: Read a => String -> a and show :: Show a => a -> String to convert from Integer and to a String:
Prelude> show 52
"52"
Prelude> read "52" :: Integer
52
But you do not need to convert values to a string to reverse the order of the digits. You can use recursion and quotRem :: Integral a => a -> a -> (a, a) to obtain the quotient and remainder.
You can also use recursing to convert an Integer to a String. Here we use as accumulator a String, and we each time calculate the last digit. This thus looks like:
fromInteger :: Integer -> String
fromInteger = go []
where go ls x | x <= 9 = … : …
| otherwise = go (… : ls) …
where (q, r) = quotRem x 10
where you still need to fill in the … parts.

How function reduction in Haskell works?

Why it's possible to reduce function in Haskell:
calculate :: Integer -> Integer -> Integer
calculate a b = f 1 a b
where
f :: Integer -> Integer -> Integer -> Integer
f a b c = a + b
into something:
calculate :: Integer -> Integer -> Integer
calculate = f 1
where
f :: Integer -> Integer -> Integer -> Integer
f a b c = a + b
I just need some guidance, any resources where I can find the answer and read more about it would be really helpful.
In Haskell there are no functions that take more than one parameter. All functions take exactly one parameter.
So if you have a function like add :: Int -> Int -> Int, then this is actually short for add :: Int -> (Int -> Int).
Why are the brackets important? Because they show how this concept works. If we have this function add, then we can make a function application with for example 14, then we construct a new function, like:
add14 :: Int -> Int
add14 = add 14
so that means that we now have a function that takes again one parameter (here an Int), and now it will produce another Int, it does that by adding 14 to it, so add14 25 will result in 39.
If you write add 14 25, this thus is not a function application with two parameters, what you actually wrote is:
-- add 14 25 is equivalent to
(add 14) 25
You thus first make a call with 14, and the you make a call with the function that comes out of this, and 25 as the parameter.
Why is this important? Because it means that if you thus write
calculate = f 1
it means that your f 1, constructs a function, a function with signature Int -> (Int -> Int). Creating parameters in the head of calculate, and adding these to the end of f 1, thus makes no sense: you already constructed a function that takes such parameters anyway. So it only introduces noise.
In lambda calculus, the rewrite rule where one rewrites λ x . f x to just f is (and vice versa) is called η-conversion [wiki]. In Haskell it comes down to rewriting:
f x = g x
to:
f = g
Operators are no different. In fact if you write a + b in Haskell, you wrote (+) a b, with (+) a function, or more verbose ((+) a) b.
The f in the where clause:
f a b c = a + b
can for example get converted to:
f = (.) ((.) const) (+)
It's called eta reduction.
You might also think about it in terms of partial application.
> calculate :: Integer -> Integer -> Integer
> f :: Integer -> Integer -> Integer -> Integer
> (f 1) :: Integer -> Integer -> Integer
Similar question - What does eta reduce mean in the context of HLint

Find the smallest number satisfying an equation

I need to solve this:
(n + a)^k < m^n
Given the a,k,m, I need to find the lowest n solving the inequality.
The only thing I came up with is:
search :: Integer -> Integer -> Integer -> Integer
search a k m =
if (inf!!0+a)^k < m^inf!!0) then inf!!0
Basically I already made a list from [0..∞] and I try to read every element starting from 0 but I can't go from the first element to second and on.
I can use extra equations with whatever type I want, but the search signature cannot be altered.
I would just use a list comprehension and head:
search :: Integer -> Integer -> Integer -> Integer
search a k m = head [ n | n <- [0..], (n + a)^k < m^n ]
The list comprehension is just syntactical sugar for
search :: Integer -> Integer -> Integer -> Integer
search a k m = head filtered
where
filtered = filter pred [0..]
pred n = (n + a)^k < m^n
If you don't want list comprehension used in #Zeta's solution, you may use dropWhile:
search :: Integer -> Integer -> Integer -> Integer
search a k m = head $ dropWhile (not.(\n -> (n + a)^k < m^n)) [0..]

Compare two functions using quickcheck to generate positive integers

I have the following Haskell functions:
expM :: Integer -> Integer -> Integer -> Integer
expM x y = rem (x^y)
And
exMME :: Integer -> Integer -> Integer -> Integer
exMME b 0 m = 1
exMME b e m = exMME' b e m 1 0 where
exMME' b e m c e'
| e' < e = exMME' b e m ((b * c) `mod` m) (e'+1)
| otherwise = c
What I want to do is use quickCheck to compare these two functions so i can see that they produce the same answer and which one is the fastest.
To test if they have the same answers I want to let QuickCheck create random positive integers with the exception of 0. So I created a Gen:
positives :: Gen Integer
positives =
do -- Pick an arbitrary integer:
x <- arbitrary
if (x == 0)
then return 1
else if (x < 0)
then return (-x)
else
return x
This works from the command line (ghci) but I have a prop:
prop_CompareAnswerExMFM :: Integer -> Integer -> Integer -> Bool
prop_CompareAnswerExMFM b e m =exMFM b e m == exM b e m
And each time i call this with QuickCheck prop_CompareAnswerExMFM it doesn't us my gen. After reading some stuff i toughed that i needed to define a instance:
instance Arbitrary Integer where
arbitrary = positives
This doesn't work because a arbitrary instance of Integer already exist. Again after some googling I say that the standard way to solve this is to use a wrapper:
newtype Positives = Positives Integer
deriving (Eq, Ord, Show)
instance Arbitrary Positives where
arbitrary = positives
positives :: Gen Positives
positives =
do -- Pick an arbitrary integer:
x <- arbitrary
if (x == 0)
then return 1
else if (x < 0)
then return (-x)
else
return x
But after playing around i keep getting errors like can't resolve this, No instance for (Num Positives) arising from the literal '0' or Can't make a derived instance of 'Num Positives'.
I think i'm making it way to complex for what i want but I just can't figure it out. I hope someone can help me or send me in the right direction.
Thanks
The problem with your code is that in positives the variable x has type Integer, so your return statements need to include the Positives constructor:
positives :: Gen Positives
positives =
do -- Pick an arbitrary integer:
x <- arbitrary
if (x == 0)
then return $ Positives 1
else if (x < 0)
then return $ Positives (-x)
else
return $ Positives x
If it helps, here is another way to write (a similarly working) positives function:
positives' :: Gen Positives
positives' = fmap (\x -> Positives (1 + abs x)) arbitrary
Here the arbitrary call is a Gen Integer, so the function argument to fmap has type Integer -> Positives.
To use your Positives newtype with QuickCheck you would use the Positives (de-)constructor to get at the Integer values:
prop_addition :: Positives -> Positives -> Bool
prop_addition (Positives a) (Positives b) = a + b >= 2
ghci> quickCheck prop_addtion
As #Carsten mentions in the comments, QuickCheck as a Positive a type which has an arbitrary instance for numeric and ordered types a.
Here's a quick way that doesn't require much understanding of QuickCheck but is a bit of a hack:
prop_CompareAnswerExMFM :: Integer -> Integer -> Integer -> Bool
prop_CompareAnswerExMFM b e m =
exMFM absB absE absM == exM absB absE absM
where -- following guarantees args are positive integers > 0
absB = 1 + abs b
absE = 1 + abs e
absM = 1 + abs m
and then you can just use
quickCheck prop_factored

Resources