简体   繁体   中英

Couldn't match expected type ‘(Int, Int)’ with actual type ‘[t0]’

why is this giving me error? I am just trying this as a test to learn Haskell where i am just strapping off the tuples in the second input. Why does this not compile ? Thank you

test :: (Int,Int) -> [(Int,Int)] -> Int
test [] [] = []
test xs [] = []
test (x,xs) (y:ys)  =   test (x,xs) ys

Thank you @Carcigenicate for pointing out the first error. Now if we have

test :: (Int,Int) -> [(Int,Int)] -> Int
test xs [] = []
test (x,xs) (y:ys)  =   test (x,xs) ys 

I get

• Couldn't match expected type ‘Int’ with actual type ‘[t0]’
• In the expression: []
  In an equation for ‘test’: test xs [] = []

Your function signature says that the first argument should be a tuple of 2 ints, but then in your first pattern matching line:

test [] [] = []
      ^

You try to match a tuple against a list.

  1. The types don't match, thus the error.

  2. A tuple will never be empty unless that's what type it is. Unlike lists, tuples can't be added to or removed from; they have a fixed size.

If you said the tuple will have 2 values, it will always have 2 values. No need to check if it's empty. It looks like you can get rid of that line, since it's not doing anything legal or useful.

Also note your naming is a little confusing. Typically, x and xs represent the head and tail of a collection. Tuples don't really have either, just different "slots".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM