What would the type of the following be? - haskell

Im just starting learn Haskell new, I'm struggling with determining the type for the following:
1) one x = [x]
2) three x = (x,x,x)
3) first x y = x
4) mult m n = m*n
5) [(False,’0’),(True,’1’)]

one x = [x] is a that takes an element and returns a list with that element only, so it has type one :: a -> [a]
three x = (x,x,x) has trivial type three :: a -> (a,a,a)
first x y = x looks like the Prelude function const that has type first :: a -> b -> a
mult m n = m*n has the same type as (*), which is (*) :: Num a => a -> a -> a
[(False,’0’),(True,’1’)] is not even a function, so has simple type :: [(Bool, Char)]
Remember you can always use they GHCi function :t to return the type of an expression

Related

Haskell: for every even appearance in an array, concatenate an int to the final list

I'm currently trying to write a function that takes as arguments an Int and an array of Ints and for every even value in the array, it concatenates the Int to the final array.
So, something like this:
f 3 [1,2,3,4,5,6] = [1,2,3,3,4,3,5,6,3]
This is the code I imagined would work (I'm just beginning so sorry if it's bad):
f :: Int -> [Int] -> [Int]
f(x,[]) = []
f(x,y)
|even head(y) = (head(y) ++ [x] ++ f(x,drop 1 y)
|otherwise = head(y) ++ f(x,(drop 1 y))
The error I'm getting is "Couldn't match expected type of 'Int' with actual type (a3, [[a3]])'. I understand the parameters types are mismatched, but I'm not sure how a proper syntax would look like here
You use (x, []), so that means the input type would be a tuple, so f :: (Int, [Int]) -> [Int].
I would also use pattern matching instead of head and tail, so:
f :: Int -> [Int] -> [Int]
f _ [] = []
f x (y:ys)
| even y = y : x : f x ys
| otherwise = y : f x ys
You can also generalize the type signature, and work with an inner function to avoid passing the x each time:
f :: Integral a => a -> [a] -> [a]
f x = go
where go [] = []
go (y:ys)
| even y = y : x : go ys
| otherwise = y : go ys
Another way of looking at this would be using a right fold to insert the desired element after even numbers.
f :: Int -> [Int] -> [Int]
f x lst = foldr (\y i -> if even y then y:x:i else y:i) [] lst
Which we can simplify to:
f :: Int -> [Int] -> [Int]
f x = foldr (\y i -> if even y then y:x:i else y:i) []
Note that without specifying the type, the more general inferred type of f would be:
f :: (Foldable t, Integral a) => a -> t a -> [a]

Counting "perfect squares" in a list in Haskell

I am new to Haskell and I am struggling to get this concept to work.
I have to use a list of integers as a parameter and count the number of perfect squares (1,4,9,etc) in the list and output that value. So for example, if I entered myfunction[1,5,9] the output would be 2.
So far I have only got one function to work:
myfunction list = map per list
where per y = floor(sqrt (y) * sqrt (y))
Takes the square root of the element multiplied by the square root of the element. My mindset with the above is that I could set the above result equal to the original element. And if they are equal to each other, that would mean they are perfect squares. And then I would increment a value.
My issue is that my book doesn't give me any ideas for how to increment a variable or how to incorporate more than 1 function at a time. And as a result, I've been aimlessly working on this over the course of 3 days.
Thank you in advance for any help, advice, or resources!
fixing your version
first completed your version could look like this:
myfunction list = length $ filter per list
where
per y = floor(sqrt y * sqrt y) == y
this would even type-check but it would not work (try it)
that's because there is a small little problem - let's make it obvious by giving some types:
myfunction :: [Int] -> Int
myfunction list = length $ filter per list
where
per :: Int -> Bool
per y = floor(sqrt y * sqrt y) == y
you get an error:
No instance for (Floating Int) arising from a use of ‘sqrt’
it is trying to say to you that it does not know how to use sqrt for an Int - an easy fix is using fromIntegral and let it convert the Int into something that can:
myfunction :: [Int] -> Int
myfunction list = length $ filter per list
where
per :: Int -> Bool
per y = floor(sqrt (fromIntegral y) * sqrt (fromIntegral y)) == y
this kind of works (wrong answer) but gives an warning - we could get rid of with
myfunction :: [Int] -> Int
myfunction list = length $ filter per list
where
per :: Int -> Bool
per y = floor(sqrt (fromIntegral y :: Double) * sqrt (fromIntegral y)) == y
were we tell Haskell what type to use for the conversion (the warning would tell you that you default to this anyway).
So there is the wrong answer still.
#jpmarinier already told why - the way you test/sqr is sadly not cutting it (at least as I thought you wanted) - here is a fix:
myfunction :: [Int] -> Int
myfunction list = length $ filter per list
where
per :: Int -> Bool
per y = let y' = (fromIntegral y :: Double) in sqrt y' ** 2 == y'
where we first convert y to a Double value y' and test this.
Another option is using a integer-sqr as #jpmarinier mentioned:
myfunction :: [Int] -> Int
myfunction list = length $ filter per list
where
per :: Int -> Bool
per y = squareRoot y * squareRoot y == y
squareRoot :: Int -> Int
squareRoot = floor . sqrt . (fromIntegral :: Int -> Double)
that should finally work.
without floor and sqr:
ok this is maybe a bit to grok for you but here is how you can do this by sieving out the values.
Let's start by creating a (ascending) list of all perfect squares - I don't know which type you want those numbers to be so let's stay generic as well:
-- need later
import Data.List (sort)
perfectSquares :: Enum a => Num a => [a]
perfectSquares = [ n*n | n <- [1..] ]
no let's make a function counting elements from two lists - if the lists are sorted this can be done recursively by walking alongside the lists - I don't know if your input lists are always sorted so let's sort it before:
countOccurances :: (Enum a, Num a, Ord a) => [a] -> [a] -> a
countOccurances from list =
countAlong from $ sort list
where
countAlong [] _ = 0
countAlong _ [] = 0
countAlong allXs#(x:xs) allYs#(y:ys)
| x < y = countAlong xs allYs
| x > y = countAlong allXs ys
| otherwise = 1 + countAlong allXs ys
having both we can combine them for the answer:
import Data.List (sort)
countPerfectSquares :: (Enum a, Num a, Ord a) => [a] -> a
countPerfectSquares = countOccurances perfectSquares
countOccurances :: (Enum a, Num a, Ord a) => [a] -> [a] -> a
countOccurances from list =
countAlong from $ sort list
where
countAlong [] _ = 0
countAlong _ [] = 0
countAlong allXs#(x:xs) allYs#(y:ys)
| x < y = countAlong xs allYs
| x > y = countAlong allXs ys
| otherwise = 1 + countAlong allXs ys
perfectSquares :: Enum a => Num a => [a]
perfectSquares = [ n*n | n <- [1..] ]
Example:
> countPerfectSquares [1,5,9] :: Int
2

Haskell: Finding the type of an expression involving map

I am trying to find the type of the following Haskell expression:
map (\x -> x + 1)
Since the map function is partially applied, it will return another function.
In GHCI, the type of the function that is returned is:
let res = map (\x -> x + 1)
:t res
map (\x -> x + 1) :: Num b => [b] -> [b]
Would the type of the above expression be the type of the function that is returned? Any insights are appreciated.
Yes, that is the type of the function which is returned.
But how does GHC figure this out? Well, let's look at the type of just plain map:
map :: (a -> b) -> [a] -> [b]
And now let's look at the type of \x -> x + 1:
(\x -> x + 1) :: Num n => n -> n
(In case you don't know yet, this means that it converts n to n, where n can be any type which is an instance of Num i.e. n is any number type.)
So matching up the types, we get:
map :: (a -> b) -> [a] -> [b]
(\x -> x + 1) :: Num n => n -> n
So:
map (\x -> x + 1) :: Num n => [n] -> [n]
Which is the same as what GHCi reports.

haskell function that sums AND multiplies numbers of a list

I'm trying to do a function that given an operator and a function will go through a list like such:
reduce (*) even [1,2,3,4,5] => 8
reduce (+) odd [1,2,3,4,5] => 9
At first I had something like this:
reduce :: (Int-> Int-> Int) -> (Int->Bool) -> [Int] -> Int
reduce op x y = foldl op 1 (filter x y)
and it worked fine for multiplying, but added one number when trying to do a sum. So I thought of adding an if:
reduce :: (Int-> Int-> Int) -> (Int->Bool) -> [Int] -> Int
reduce op x y =
if op == (*) then foldl op 1 (filter x y)
else if op == (+) then foldl op 0 (filter x y)
else 0
However when I do this I get the following error:
Instance of Eq (Int -> Int -> Int) required for definition of reduce
What does it mean exactly? Any ideas? Thanks in advance
It means that with your current imports you cannot compare functions for equality (and even with suitable imports, there is no really good way to do the equality you ask for).
One simple solution is to demand that the user provide both the operation and the unit, thus:
reduce op u x y = foldl op u (filter x y)
In many cases of interest, there is already a Monoid instance that specifies the operator and unit. For example, there are the Sum and Product instances, so you might consider writing
reduce inject p = foldMap inject . filter p
which would be used like this:
> reduce Sum even [1..5]
Sum {getSum = 6}
> reduce Product odd [1..5]
Product {getProduct = 15}
What does it mean exactly? Any ideas? Thanks in advance
You're trying to compare some (Int -> Int -> Int) to another one. But functions cannot get checked on equality. Provide the initial value instead:
reduce :: (Int -> Int -> Int) -> (Int -> Bool) -> Int -> [Int] -> Int
reduce op f x xs = foldl op x (filter f xs)
By the way, you can generalize your function's type:
reduce :: (b -> a -> b) -> (a -> Bool) -> b -> [a] -> a
reduce op f x xs = foldl op x (filter f xs)
Alternatively, use a monoid and foldMap.

declare function type in function definition in haskell

I just began to learn haskell recently. I have some confusion of declare function type in the function definition. For example if we want to define a foldr function:
foldr :: (a->b->b)->b->[a]->b
foldr f v [] = v
foldr f v (x:xs) = f x (foldr f v xs)
Thus, foldr (+) 0 [1,2,3,4] = sum [1,2,3,4] = 10.
My question is: can I just change it to something like :
foldr :: (a->a->a)->a->[a]->a
OR:
foldr :: (a->b->c)->x->[y]->z
I didn't find a place to explain the choice/requirements of letters in the definition. Thanks!
Update:
I tested it with:
foldr :: (a->b->c)->x->[y]->z
Got the following err:
Couldn't match type `y' with `a'
`y' is a rigid type variable bound by
the type signature for folderr :: (a -> b -> c) -> x -> [y] -> z
at test.hs:3:1
`a' is a rigid type variable bound by
the type signature for folderr :: (a -> b -> c) -> x -> [y] -> z
at test.hs:3:1
If changed everything to a, it works. I must have missing some points here.
As explained in #sepp2k's comments. I tested using the following simple example:
folderr :: (a->b->b)->b->[a]->b
folderr f v [] = v
folderr f v (x:xs) = f x (folderr f v xs)
myop :: Int -> Bool -> Bool
myop x y | x > 0 = True && y
| otherwise = False && y
folderr (myop) True [3,3,222] -> True
folderr (myop) True [3,2,-1] -> False
So a,b just means the types are different or not.
The specific names of your type variables do not matter, that is the types a -> b -> c, x -> y -> z and foo -> bar -> baz are all completely equivalent.
What does matter is whether you use the same type variable multiple times or you use different ones. That is a -> b is a different type than a -> a. Specifically a -> b is more general because out of the concrete types Int -> Int, String -> String, Int -> String and String -> Int, a -> a would only match the first two (with a = Int and a = String respectively) whereas a -> b would match all four.

Resources