1

Hello can someone explain to me why this custom implementaiton of foldl is not in scope?

afold::(a->b->a)->a->[b]->a
afold tsf accu (x:xs)=afold tsf (tsf accu x) xs
afold _ accu []=accu

I've tried running it like this :

afold (\x y-> show y:x) [] [1,2,3,4]

and i get the error:

Variable not in scope:
      afold :: ([String] -> () -> [String]) -> [a0] -> [Integer] -> t

When i try to use it even simpler like this :

 afold (\x y-> x+y) 0 [1,2,3,4]

i get the error:

 :: (Integer -> Integer -> Integer) -> Integer -> [Integer] -> t

Why isn't the output inferred?Why is it still t in the second example?

5
  • 2
    "Not in scope" means that it can't find any definition. It's not a type error, it's an import error.
    – Carl
    Commented Apr 30, 2018 at 5:17
  • Well why would i need anything imported since i'm using only primitive functions. Commented Apr 30, 2018 at 5:22
  • 5
    @BercoviciAdrian Where is your implementation of afold and where are you trying to run it? The "Not in scope" error is because it cannot find afold at all. As Carl said, the type section of that error is not very significant (in this particular context). Commented Apr 30, 2018 at 6:04
  • :: (Integer -> Integer -> Integer) -> (...) isn't an error, that's a type signature. Could you actually write the full, unedited error?
    – AJF
    Commented Apr 30, 2018 at 8:21
  • I solved it.I didn't place it in a module to load it. Commented Apr 30, 2018 at 8:32

1 Answer 1

3

The problem was that i didn't place the method inside a module ,and load it.Once i enclosed the method in a module and loaded it in ghci it worked.

module Test where
afold::(a->b->a)->a->[b]->a
afold tsf accu (x:xs)=afold tsf (tsf accu x) xs
afold _ accu []=accu

In GHCI:

:load Test

Not the answer you're looking for? Browse other questions tagged or ask your own question.