'haskell Differences in two words [closed]
I have 2 words with the same length, for example: "mother" and "father". My program should:
a) if the letter in the x position in the first word is the same as in the x position in the second word: the letter should stay on its place
b) if the letter in the x position in the first word isn't the same as in the x position in the second word: the letter should be replaced with sign "-"
function mother father
--result "--ther"
So far I made this:
function :: (Eq a) => [a] -> [a] -> [a]
function [] [] = []
function [x] [] = [x]
function [] [x] = [x]
function (x:xs) (y:ys) = if x == y
then x : function xs ys
else
"-" : function xs ys
It gives error
Couldn't match type `a' with `[Char]
I have no clue how make last line works. Do you have any idea what should I do here?
Solution 1:[1]
You use "-", hence that means that the result is a list of Strings. You probably want to use '-', then it is a list of Chars, so a String:
function :: String -> String -> String
function x [] = x
function [] x = x
function (x:xs) (y:ys)
| x == y = x : function xs ys
| otherwise = '-' : function xs ys
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Willem Van Onsem |
