Higher order functions in haskell error - haskell

selectMenu :: Int->IO()
selectMenu num
|(num==1)=convertFromDecimal
|(num==2)=--menu2
|(num==3)=putStrLn("3")
|(num==4)=putStrLn("4")
|(num==5)=putStrLn("5")
convertFromDecimal:: IO()
convertFromDecimal= do
putStrLn("\n\tConvert From Decimal To Binary & Octals \n")
putStrLn("----------------------------------------------------------\n")
putStrLn("Enter 5 decimal numbers [,,] : ")
input<-getLine
let n=(read input)::[Int] -- is this right?
--putStrLn (show n)
let result = convertionTO decToOct n
putStrLn(show result)`
decToOct :: Int -> [Int]
decToOct x = reverse(decToOct' x)
where
decToOct' 0 = []
decToOct' y = let (a,b) = quotRem y 8 in [b] ++ decToOct' a
convertionTO :: (Int -> [Int] -> [Int]) -> [Int] -> [Int]
convertionTO _ [] = []
convertionTO f (x:xs) = f x : convertionTO f xs
I correct those mistakes. I did update the question after correcting those errors. But this time it gives this error
How can i fix this error?
Assignment.hs:49:51:
Couldn't match expected type `[Int] -> [Int]'
with actual type `[Int]'
Expected type: Int -> [Int] -> [Int]
Actual type: Int -> [Int]
In the first argument of `convertionTO', namely `decToOct'
In the expression: convertionTO decToOct n
Assignment.hs:66:25:
Couldn't match expected type `Int'
with actual type `[Int] -> [Int]'
In the return type of a call of `f'
In the first argument of `(:)', namely `f x'
In the expression: f x : convertionTO f xs

(I'm copying the errors here in case you edit the question again.)
The first error
Assignment.hs:49:51:
Couldn't match expected type `[Int] -> [Int]'
with actual type `[Int]'
Expected type: Int -> [Int] -> [Int]
Actual type: Int -> [Int]
In the first argument of `convertionTO', namely `decToOct'
In the expression: convertionTO decToOct n
refers to this line of code
let result = convertionTO decToOct n
conversionTO expects its first argument to have the type Int -> [Int] -> [Int], but decToOct instead has the type Int -> [Int].
The second error
Assignment.hs:66:25:
Couldn't match expected type `Int'
with actual type `[Int] -> [Int]'
In the return type of a call of `f'
In the first argument of `(:)', namely `f x'
In the expression: f x : convertionTO f xs
refers to this line of code
convertionTO f (x:xs) = f x : convertionTO f xs
convertionTO produces an [Int], so the first argument to : must be an Int. But f x instead has type [Int] -> [Int].
How to fix these?
I'm going to assume that your line 49 is correct, and that the type signature for convertionTO is wrong. In that case it should be
convertionTO :: (Int -> [Int]) -> [Int] -> [Int]
This doesn't fix the second error, as f x now has type [Int].
The problem here is your use of :. : joins a single element to the start of a list. What you have is two lists that you want to join together. For this, use ++.
convertionTO f (x:xs) = f x ++ convertionTO f xs
Finally, note that your convertionTO is in the standard library as concatMap, which combines map with concat.

Those errors mean that you're referring to functions which don't exist. Either because you mistyped their names, or because you haven't defined them yet.
For example, instead of showresult, you probably meant show result, and I think you meant decToOct' instead of decToAny'. I can't see any references to convertToDecimal anywhere in the code you posted, so that error is probably somewhere else.

Related

Move One Element From one list to another In haskell

Morning i was doing an exercise it's my first time in Haskell
The exercise is to move the first element from my second list to the first position of my first list
Result: test [] [4,2,5,7]
=> ([4], [2,5,7])
That is my code :
test ::[a] -> [b] -> ([a],[b])
test [] [] = ([], [])
test [x] [] = ([x], [])
test [] [x] = ([x], [])
test [y] [x] = ([x:y], [])
But i got errors so please help me
this is my error
• Couldn't match expected type ‘[b]’ with actual type ‘a’
‘a’ is a rigid type variable bound by
the type signature for:
pa :: forall a b. [a] -> [b] -> ([a], [b])
at Pushswap.hs:30:6
• In the second argument of ‘(:)’, namely ‘y’
In the expression: x : y
In the expression: [x : y]
• Relevant bindings include
x :: b (bound at Pushswap.hs:34:9)
y :: a (bound at Pushswap.hs:34:5)
pa :: [a] -> [b] -> ([a], [b]) (bound at Pushswap.hs:31:1)
Failed, modules loaded: none.
In the implementation
test [y] [x] = ([x:y], [])
y is of type a, x is of type b and [x:y] needs to be of type [a], hence the error.
You should be able to get past this error by making both lists the same type.

Couldn't match expected type [Int] Haskell

I am new to Haskell so I don't quite understand most of its errors. I have encountered an error trying to make a higher order function that uses foldl() to read a list and then multiply it by 2 (it automatically reverses it) and I use another another foldl() just to read it, in so reversing it to its original order.
I would be thankful for any help I receive.
here's the error
• Couldn't match expected type ‘[Int]’
with actual type ‘t0 Integer -> [Integer]’
• Probable cause: ‘foldl’ is applied to too few arguments
In the first argument of ‘reverses’, namely
‘(foldl (\ acc x -> (2 * x) : acc) [])’
In the expression: reverses (foldl (\ acc x -> (2 * x) : acc) [])
In an equation for ‘multthr’:
multthr = reverses (foldl (\ acc x -> (2 * x) : acc) [])
|
16 | multthr = reverses(foldl(\acc x-> (2*x):acc)[])
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
here's the source code
reverses::[Int] ->[Int]
reverses = foldl(\acc x-> x:acc)[]
multthr::[Int]->([Int]->[Int])
multthr = reverses(foldl(\acc x-> (2*x):acc)[])
You need to compose your two foldl's with (.), instead of applying one to the other:
reverses :: [a] -> [a]
reverses = foldl (\acc x-> x:acc) []
---- multthr :: [Int] -> ([Int] -> [Int]) -- why??
multthr :: [Int] -> [Int]
---- multthr = reverses (foldl(\acc x-> (2*x):acc)[]) -- causes error
multthr = reverses . foldl (\acc x-> (2*x):acc) []
so that we have
> multthr [1..5]
[2,4,6,8,10]
it :: [Int]
(.) is the functional composition, defined as (f . g) x = f (g x).
Instead of doing the two foldl's you can do one foldr,
mult2 = foldr (\x r -> (2*x) : r) []
but then this is nothing else but, simply, map (2*).

Haskell : Possible cause: ‘(:)’ is applied to too many arguments

I‘ve created a nested function which fails at the third nesting. I fail to understand why this pattern holds up for the previous two nests but not the third. I'm attempting to recursively work through the list to add the items in the list together
module Nests
(nest1,nest2,nest3) where
nest1 :: (Num a) => [a] -> [a] -> [a]
nest1 _ [] = []
nest1 [] _ = []
nest1 (xs:ls1) (xt:ls2) = (xs+xt):nest1 ls1 ls2
nest2 :: (Num a) => [[a]] -> [[a]] -> [[a]]
nest2 _ [] = []
nest2 [] _ = []
nest2 (xs:ls1) (xt:ls2) = (nest1 xs xt):nest2 ls1 ls2
nest3 :: (Num a) => [[[a]]] -> [[[a]]] -> [[[a]]]
nest3 (xs:xt:ls)
| length ls <= 0 = []
| otherwise = (nest2 xs xt):nest3 ls
where compiled:
nests.hs:17:19: error:
• Couldn't match expected type ‘[[[a]]] -> [[[a]]]’
with actual type ‘[[[a]]]’
• Possible cause: ‘(:)’ is applied to too many arguments
In the expression: (nest2 xs xt) : nest3 ls
In an equation for ‘nest3’:
nest3 (xs : xt : ls)
| length ls <= 0 = []
| otherwise = (nest2 xs xt) : nest3 ls
• Relevant bindings include
ls :: [[[a]]] (bound at nests.hs:15:14)
xt :: [[a]] (bound at nests.hs:15:11)
xs :: [[a]] (bound at nests.hs:15:8)
nest3 :: [[[a]]] -> [[[a]]] -> [[[a]]] (bound at nests.hs:15:1)
|
17 | | otherwise = (nest2 xs xt):nest3 ls
| ^^^^^^^^^^^^^^^^^^^^^^
nests.hs:17:33: error:
• Couldn't match expected type ‘[[[a]]]’
with actual type ‘[[[a]]] -> [[[a]]]’
• Probable cause: ‘nest3’ is applied to too few arguments
In the second argument of ‘(:)’, namely ‘nest3 ls’
In the expression: (nest2 xs xt) : nest3 ls
In an equation for ‘nest3’:
nest3 (xs : xt : ls)
| length ls <= 0 = []
| otherwise = (nest2 xs xt) : nest3 ls
• Relevant bindings include
ls :: [[[a]]] (bound at nests.hs:15:14)
xt :: [[a]] (bound at nests.hs:15:11)
xs :: [[a]] (bound at nests.hs:15:8)
nest3 :: [[[a]]] -> [[[a]]] -> [[[a]]] (bound at nests.hs:15:1)
|
17 | | otherwise = (nest2 xs xt):nest3 ls
Type annotation for nest3 does not match its body.
nest3 :: [[[a]]] -> [[[a]]] -> [[[a]]] means that function takes two arguments before returning list but in the function's body you take just one argument.
You can need to fix type signature (or just drop it, GHC is able to infer it):
nest3 :: (Num a) => [[[a]]] -> [[[a]]]
nest3 (xs:xt:ls)
| length ls <= 0 = []
| otherwise = (nest2 xs xt):nest3 ls
The problem here is that the first error seems not very relevant. The second one clearly states that nest3 expects one more argument.
• Couldn't match expected type ‘[[[a]]]’
with actual type ‘[[[a]]] -> [[[a]]]’
• Probable cause: ‘nest3’ is applied to too few arguments
But the first one suggest that (:) is used incorrectly.
• Couldn't match expected type ‘[[[a]]] -> [[[a]]]’
with actual type ‘[[[a]]]’
• Possible cause: ‘(:)’ is applied to too many arguments
To understand why there are two errors and why the first one is so obscure, consider how GHC works. It tries both to infer types and match inferred types with type annotations provided by the programmer.
Knowing that (:) :: b -> [b] -> [b] we can infer that nest3 ls must be of type [b] in the expression (..) : nest3 ls. Matching this with the provided type [[[a]]] -> [[[a]]] -> [[[a]]] gives the error ‘nest3’ is applied to too few arguments.
Trying to infer type of the otherwise branch in nest3 GHC considers again the type of (:). Applied to two arguments (:) gives a list. Matching this with the provided nest3 type gives ‘(:)’ is applied to too many arguments because only by removing one of the arguments we can get an arrow type.
Regarding the question of why it says ‘(:)’ is applied to too many arguments:
Since your type signature tells Haskell that the function takes two arguments, but you only match for one, it will interpret it as a function returning a function, kind of like in point free style
For example like this
plus :: Int -> Int -> Int
plus x = (+) x
which is equivalent to these
plus' :: Int -> Int -> Int
plus' x y = (+) x y
plus'' :: Int -> Int -> Int
plus'' = (+)
So, in this case, your function is equivalent to
nest3 :: (Num a) => [[[a]]] -> [[[a]]] -> [[[a]]]
nest3 (xs:xt:ls) z
| length ls <= 0 = [] z
| otherwise = ((nest2 xs xt):nest3 ls) z
-- or
-- | otherwise = (:) (nest2 xs xt) (nest3 ls) z
Which explains why it thinks (:) might be applied to too many arguments.

Problems with Haskell variable scope error and creating a proper type declaration

In most of my functions, I seem to get the common error of "Variable not in scope"
For example this function
zipmap :: [Int]->[Int]->[Int]
zipmap x y = map (\(a,b) -> a + b) (x zip y)
would give me "Variable not in scope: zipmap :: [Integer] -> [Integer] -> t"
Why is this happening, and for future reference what can I do to avoid these errors?
My guess is that there are already errors when you define the function. Since you fail to define the zipmap function, the interactive shell assumes nothing happened, and thus it does not know about a function zipmap later in the process. For example:
$ ghci
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
Prelude> zipmap :: [Int]->[Int]->[Int]; zipmap x y = map (\(a,b) -> a + b) (x zip y)
<interactive>:1:68: error:
• Couldn't match expected type ‘([a0] -> [b0] -> [(a0, b0)])
-> [Int] -> [(Int, Int)]’
with actual type ‘[Int]’
• The function ‘x’ is applied to two arguments,
but its type ‘[Int]’ has none
In the second argument of ‘map’, namely ‘(x zip y)’
In the expression: map (\ (a, b) -> a + b) (x zip y)
Prelude> zipmap [1,2,3] [1,4,2,5]
<interactive>:2:1: error:
Variable not in scope: zipmap :: [Integer] -> [Integer] -> t
Note the part in boldface: the interpreter says it does not understand what you say, so it can not proceed with that. Then later if we use zipmap, it raises the same error like the one you describe.
Your zipmap function is wrong since you write:
x zip y
zip :: [a] -> [b] -> [(a,b)] is a function, not an operator, so you should use it as:
zip x y
Or in full:
zipmap :: [Int] -> [Int] -> [Int]
zipmap x y = map (\(a,b) -> a + b) (zip x y)
We can further improve this: there is a zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] function:
zipmap :: [Int] -> [Int] -> [Int]
zipmap x y = zipWith (+) x y
or even shorter:
zipmap :: [Int] -> [Int] -> [Int]
zipmap = zipWith (+)
and finally we can generalize our function:
zipmap :: Num a => [a] -> [a] -> [a]
zipmap = zipWith (+)

Haskell String to Maybe List

readSquareTransition :: String -> Maybe [SquareTurn]
readSquareTransition [] = Just []
readSquareTransition (x:xs) = case x of
'L' -> Just (L : readSquareTransition xs)
'R' -> Just (R : readSquareTransition xs)
_ -> Nothing
I want to get Just [L,L,R,R]. But looks like i failed :( Here is the error message!
src/StudentSources/LangtonsAnt.hs:231:24:
Couldn't match expected type ‘[SquareTurn]’
with actual type ‘Maybe [SquareTurn]’
In the second argument of ‘(:)’, namely ‘readSquareTransition xs’
In the first argument of ‘Just’, namely
‘(L : readSquareTransition xs)’
src/StudentSources/LangtonsAnt.hs:232:24:
Couldn't match expected type ‘[SquareTurn]’
with actual type ‘Maybe [SquareTurn]’
In the second argument of ‘(:)’, namely ‘readSquareTransition xs’
In the first argument of ‘Just’, namely
‘(R : readSquareTransition xs)’
A modular way of doing this would be to define readSquareTurn first which defines how to turn a Char into a single SquareTurn (with the possibility of failure):
readSquareTurn :: Char -> Maybe SquareTurn
readSquareTurn x = case x of
'L' -> Just L
'R' -> Just R
_ -> Nothing
and then use mapM :: (a -> Maybe b) -> [a] -> Maybe [b] to process the whole String like so:
readSquareTransition :: String -> Maybe [SquareTurn]
readSquareTransition = mapM readSquareTurn
Change this
'L' -> Just (L : readSquareTransition xs)
'R' -> Just (R : readSquareTransition xs)
to this
'L' -> fmap (L :) $ readSquareTransition xs
'R' -> fmap (R :) $ readSquareTransition xs
The problem is that readSquareTransition returns a Maybe [SquareTurn], so you can't apply (:) to it ((:) requires a List). fmap however lets you apply into the Just (while preserving a Nothing).

Resources