-1

I want to create a union function for two images (created using lists of type string.) I have started by creating a unionList function that combines two lists.

unionList :: String -> String -> String  
unionList xs ys =  xs ++ foldl (flip delete) ys xs

This works but not how i want it too.
I want it to work like:

input = unionList [’ ’,’ ’,’X’,’X’,’ ’] [’X’,’ ’,’X’,’ ’,’X’] 
output = "X XXX"

any ideas on how it's done to achieve this.

EDIT: I'm ultimately trying to create a union of two images.

image 1 = ["  XX  ",     image 2 = ["XX XX",         type Img = [String]
           " X  X ",                "  X  ",
           "  XX  "]                "XX XX"]          

^
examples

Input = (imgUnion (image 1) (image 2))

should give me the union of the two images.
imgUnion defined as

imgUnion :: Img -> Img -> Img
9
  • Start with a solution that only works on lists of length 1 and uses pattern matching (on lists and Char literals). Google "pattern matching haskell" if you don't understand what I mean. Then add more cases (to match the empty list) and a recursive call to unionList to support lists of arbitrary length.
    – jberryman
    Apr 6, 2016 at 15:29
  • 3
    Look at zipWith. Apr 6, 2016 at 15:29
  • @ReinHenrichs - I looked into zipWith and will use it for the image union. but i want to get the unionList function to work as defined above first and use it with zipWith.
    – evian1
    Apr 6, 2016 at 15:32
  • unionList is the function that I am suggesting you use zipWith for. The function as defined above is wrong, and I don't know how to make it work, since it is wrong. Apr 6, 2016 at 15:33
  • 1
    Try doing it with zipWith. I don't want to just give you the answer because this is homework. Apr 6, 2016 at 15:45

1 Answer 1

1

There are probably better ways to do this, but I had some fun trying out a couple of methods.

Pattern matching on list head item:

unionList1 :: String -> String -> String
unionList1 ('X':xs) ( _ :ys) = 'X' : unionList1 xs ys
unionList1 ( _ :xs) ('X':ys) = 'X' : unionList1 xs ys
unionList1 ( _ :xs) ( _ :ys) = ' ' : unionList1 xs ys
unionList1     _        _    = []

Guards!

unionList2 :: String -> String -> String
unionList2 (x:xs) (y:ys)
  | x == ' '   = y : rest
  | y == ' '   = x : rest
  | otherwise  = x : rest
  where rest   = unionList2 xs ys
unionList2 _ _ = []

And a solution as proposed with zipWith:

unionList3 :: String -> String -> String
unionList3 = zipWith (\x y -> if x /= ' ' then x else y)

You can then use one of these union functions on the image:

imgUnion :: [String] -> [String] -> [String]
imgUnion = zipWith unionList3

main :: IO ()
main = do
    let img1 = ["  XX  ",
                " X  X ",
                "  XX  "]
        img2 = ["XX XX",
                "  X  ",
                "XX XX"]

    mapM_ putStrLn (imgUnion img1 img2)

Gives as output:

XXXXX
 XX X
XXXXX
2
  • It is for the UnionList function. How will i include this for the image union function. (edit in question)
    – evian1
    Apr 6, 2016 at 16:29
  • I've edited my answer to include your edited question, is this what you're looking for? Apr 6, 2016 at 16:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.