1

I was given merge as below:

merge :: Ord a => [a] -> [a] -> [a]
merge xs [] = xs    
merge [] ys = ys    
merge (x:xs) (y:ys)
    | x == y    = x : merge xs ys    
    | x <= y    = x : merge xs (y:ys)
    | otherwise = y : merge (x:xs) ys

and

type Var = String

now I want to merge ['a' .. 'z'] with [1....] to create infinite list of variables. ['a1','b1','c1'...'z1','a2','b2',...'z2',...]

I have defined my variables as below:

variables :: [Var]
variables = merge ['a'..'z'][1..]

But I'm getting below error:

error:
    • Couldn't match type ‘Char’ with ‘[Char]’
      Expected type: [Var]
        Actual type: [Char]
    • In the expression: merge ['a' .. 'z'] [1 .. ]
      In an equation for ‘variables’:
          variables = merge ['a' .. 'z'] [1 .. ]
   |
50 | variables = merge ['a'..'z'][1..]
   |             ^^^^^^^^^^^^^^^^^^^^^

what am I getting wrong here ? Please help...thanks in advance!

amy_lee
  • 33
  • 6
  • The two items should be of the same type. – Willem Van Onsem Mar 24 '21 at 11:33
  • 2
    You could convert both input lists into lists of String first. Those can then be merged. Or write a different function that is specialised to take two lists of the types you have here (Char and Int) – Thilo Mar 24 '21 at 11:36
  • 2
    The `merge` here aleso does not look like it will create a list with `a1`, `b1`, etc. It simply will make an ordered list that is the union of two other ordered lists. – Willem Van Onsem Mar 24 '21 at 11:37
  • 2
    `merge [a,b] [1,2]` will return `[1,2,a,b]` not `[a1, b1, a2, b2]` – Thilo Mar 24 '21 at 11:45
  • 4
    For your task, the `merge` function seems to be useless. I'd forget about using that, and search for some other alternative e.g. using a list comprehension. – chi Mar 24 '21 at 11:59
  • Thanks very much all, I will convert both into the same data type and try to use list comprehension. – amy_lee Mar 24 '21 at 19:30

0 Answers0