3

NOTE: prohibited from using ++ in your code.

join :: ([t],[t]) -> [t]

which takes a pair of lists of some type t, (xs,ys) and returns a single list that contains all the elements some testcases :

join ([],[])) "==" [])
join ([],[5,6,5])) "==" [5,6,5]
join ([7],[5,6,5])) "==" [7,5,6,5]
join ([3,7],[5,6,5])) "==" [3,7,5,6,5]
join ("be","happy")) "==" "behappy"

This is what I currently have

module Sum where 
join :: ([t],[t]) -> [t]
join x y = [ z | z <- x, a <- y]

the error I get is

Sum.hs:3:1: error:
    * Couldn't match expected type `[t]'
                  with actual type `[t0] -> [a0]'
    * The equation(s) for `join' have two arguments,
      but its type `([t], [t]) -> [t]' has only one
    * Relevant bindings include
        join :: ([t], [t]) -> [t] (bound at Sum.hs:3:1)
  |
3 | join x y = [ z | z <- x, a <- y]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sum.hs:3:23: error:
    * Couldn't match expected type `[a0]' with actual type `([t], [t])'
    * In the expression: x
      In a stmt of a list comprehension: z <- x
      In the expression: [z | z <- x, a <- y]
    * Relevant bindings include
        x :: ([t], [t]) (bound at Sum.hs:3:6)
        join :: ([t], [t]) -> [t] (bound at Sum.hs:3:1)
  |
3 | join x y = [ z | z <- x, a <- y]
  |                       ^

2 Answers 2

5

First of all, you have a type error in the arguments of the function:

join :: ([t],[t]) -> [t]
join x y = [ z | z <- x, a <- y]

it should be:

join :: ([t],[t]) -> [t]
join (x,y) = [ z | z <- x, a <- y]

but that will return to you:

  join' ("hello", "world")
=> "hhhhheeeeellllllllllooooo"

because list comprehension combines all values from the two lists, so, don't use it in this case. Instead:

Why don't you use recursion over the structure of (,) and []? it should be easy as:

join :: ([a], [a]) -> [a]
join ([], ys) = ys
join (xs, []) = xs
join ((x:xs), ys) = x : (join (xs, ys))

example:

join ("hello", "world")
=> "helloworld"
Sign up to request clarification or add additional context in comments.

Comments

1

They want you to implement ++.

If you could use ++, the implementation would be:

join (x, y) = x ++ y

1 Comment

I saw that before the edit which is why I wrote the answer the way I did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.