Home > Net >  Couldn't match type '[t0 -> t]' -> Bool with Bool
Couldn't match type '[t0 -> t]' -> Bool with Bool

Time:11-24

I am a beginner in haskell. I am facing the error that's in the heading. Below is my haskell code.

member atm lizt = if null lizt
                     then False
                     else if atm == head lizt then True else member(atm (tail lizt))
                     
main = print(member 1 [2, 1])

The error message

    GHCi, version 8.10.6: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/runner/member/.ghci
[1 of 1] Compiling Main             ( Main.hs, interpreted )

Main.hs:1:1: error:
    • Couldn't match type ‘[t0 -> t] -> Bool’ with ‘Bool’
      Expected type: t -> Bool
        Actual type: (t0 -> t) -> [t0 -> t] -> Bool
    • Relevant bindings include
        member :: t -> Bool (bound at Main.hs:1:1)
  |
1 | member atm lizt = if null lizt
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

Main.hs:5:14: error:
    • Couldn't match expected type ‘[a1] -> a0’ with actual type ‘Bool’
    • The function ‘member’ is applied to two arguments,
      but its type ‘t1 -> Bool’ has only one
      In the first argument of ‘print’, namely ‘(member 1 [2, 1])’
      In the expression: print (member 1 [2, 1])
  |
5 | main = print(member 1 [2, 1])
  |              ^^^^^^^^^^^^^^^
Failed, no modules loaded.
 
<interactive>:1:1: error:
    • Variable not in scope: main
    • Perhaps you meant ‘min’ (imported from Prelude)
 ^C Leaving GHCi.
repl process died unexpectedly: 
GHCi, version 8.10.6: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/runner/member/.ghci
 

I am trying to print true if the given integer(atm in my case) is present in the given list else false. The code is working(justing checking with the first element of the list) before I add recursion call. Please help me with this.

CodePudding user response:

I think you have a misconception in how functions are called.

member is a two argument function. It can be called like member atm (tail lizt). When you try to call it like member(atm (tail lizt)), atm (tail lizt) tells it to call atm as a function on tail lizt. But atm isn't supposed to be a function. What you want is to call member with two arguments atm and tail lizt.

The compiler error you get is not that great. The compiler knows something is wrong, but it doesn't know exactly what. These sorts of confusing errors happen a lot in Haskell. The best way to approach them is to notice that you have a type error, and then start adding type signatures. If you add the type signature you want for member to your code:

member :: Eq a => a -> [a] -> Bool

Then the compiler has a better idea of what you are trying to do and gives a more helpful answer:

    • Couldn't match expected type ‘Bool’
                  with actual type ‘[a0] -> Bool’
    • Probable cause: ‘member’ is applied to too few arguments
      In the expression: member (atm (tail lizt))
      In the expression:
        if atm == head lizt then True else member (atm (tail lizt))
      In the expression:
        if null lizt then
            False
        else
            if atm == head lizt then True else member (atm (tail lizt))
  |
4 |                      else if atm == head lizt then True else member(atm (tail lizt))
  |                                                              ^^^^^^^^^^^^

It also helps if you just add type signatures to begin with, since they help communicate what your code is supposed to do to other human beings as well.

  • Related