Juha R
Juha R

Reputation: 11

My Haskell code has type annotation errors and can't figure out what those are

Here is the code I wrote:

multiApp :: (a -> a) -> [a -> a] -> a -> [a]
multiApp f [] x = []
multiApp f gs x = f $ multiAppi gs x

multiAppi :: [a -> a] -> a -> [a]
multiAppi [] x = []
multiAppi gs x = ((head gs) x) : multiAppi (tail gs) x

I'm trying to use list of functions to a value and then use function f to that list.

Examples:

multiApp id [] 7  ==> []

multiApp id [id, reverse, tail] "This is a test" ==> ["This is a test","tset a si sihT","his is a test"]

multiApp id  [(1+), (^3), (+2)] 1  ==>  [2,1,3]

multiApp sum [(1+), (^3), (+2)] 1  ==>  6

multiApp reverse [tail, take 2, reverse] "foo" ==> ["oof","fo","oo"]

multiApp concat [take 3, reverse] "race" ==> "racecar"

Here is answer:

Set3a.hs:269:34: error:
    * Occurs check: cannot construct the infinite type: a ~ [a]
    * In the second argument of `(:)', namely `multiAppi (tail gs) x'
      In the expression: ((head gs) x) : multiAppi (tail gs) x
      In an equation for `multiAppi':
          multiAppi gs x = ((head gs) x) : multiAppi (tail gs) x
    * Relevant bindings include
        x :: a (bound at Set3a.hs:269:14)
        gs :: [a -> a] (bound at Set3a.hs:269:11)
        multiAppi :: [a -> a] -> a -> a (bound at Set3a.hs:268:1)
    |
269 | multiAppi gs x = ((head gs) x) : multiAppi (tail gs) x
    |                                  ^^^^^^^^^^^^^^^^^^^^^

What infinite type? What???

Edit: Now code reads like this:

multiApp :: ([a] -> b) -> [a -> a] -> a -> b
multiApp f gs x = f $ multiAppi gs x

multiAppi :: [a -> a] -> a -> [a]
multiAppi [] x = []
multiAppi (g:gs) x = g x : multiAppi gs x

And errors for sum function is like:

set3atest.hs:232:42: error: * Couldn't match type Int' with [Int]' Expected type: Int -> Int Actual type: [Int] -> Int * In the expression: head In the second argument of multiApp', namely [head, last]' In the first argument of (?==)', namely multiApp (sum :: [Int] -> Int) [head, last] [1 :: Int, 2, 3, 4]' | 232 | multiApp (sum::[Int]->Int) [head, last] [1::Int,2,3,4] ?== 5 | ^^^^

And this stackoverflow demands less code and more comments. I don't know how to more comment this...

Upvotes: 1

Views: 145

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476729

I can not reproduce this error, there is a problem with your multiApp, but not multiAppi. Based on the error message, you used [a -> a] -> a -> a instead of [a -> a] -> a -> [a]. You thus should rewrite the signature such that the output is a list of as, and reload the file.

Another problem is that for multiApp your first function should take a list of items [a] and convert it to an item b, so the signature should be:

multiApp :: ([a] -> b) -> [a -> a] -> a -> b
multiApp f gs x = f (multiAppi gs x)

your multiAppi can be rewritten by making use of a pattern:

multiAppi :: [a -> a] -> a -> [a]
multiAppi [] x = []
multiAppi (g:gs) x = g x : multiAppi gs x

or simpler with a map:

multiAppi :: [a -> a] -> a -> [a]
multiAppi gs x = map ($ x) gs

Upvotes: 1

Related Questions