Making a parse error when learning about Index functions

Hello, I was following this tutorial FP 9 - Exercises On Recursion - YouTube
when I came across a specific a parse error in the code

(!!) :: [a] -> Int -> a 
(x:_) !! 0 = x
(_:xs) !! n = xs !! (n-1)

I tried using Prelude.!! but that created errors for the rest of my code.

Any suggestions?

Your code actually parses fine, but the next step after parsing is name resolution, i.e. working out what each name in your program refers to, and that fails. The problem is that the (implicitly imported) Prelude module also defines a function called (!!), so it is ambiguous which one is being referenced.

Two possible solutions:

  • Rename your (!!) function to something that doesn’t conflict with the imported name.
  • Hide the imported function by adding a line import Prelude hiding ((!!)), after the module ... where line if there is one.
1 Like

there isn’t a module line. I’m not really building a large program right now, I’m just experimenting with different recursive functions

Prelude is implicitly imported. If you do not add module lines, it is like your program starts with:

import Prelude

yeah that throws off my other functions

What throws off your other functions? Can you paste your error in full?