haskell 两个词的区别

haskell Differences in two words

我有两个相同长度的单词,例如:“妈妈”和“爸爸”。我的程序应该:

a) 如果第一个单词中x位置的字母与第二个单词中x位置的字母相同:字母应该留在原处

b) 如果第一个单词中 x 位置的字母与第二个单词中 x 位置的字母不相同:该字母应替换为符号“-”

function mother father
--result "--ther"

到目前为止我做了这个:

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

报错

Couldn't match type `a' with `[Char]

我不知道最后一行是如何工作的。你知道我应该在这里做什么吗?

您使用 "-",因此这意味着结果是 String 的列表。你可能想使用 '-',那么它是一个 Char 的列表,所以 String:

function :: <strong>String</strong> -> <strong>String</strong> -> <strong>String</strong>
function x [] = x
function [] x = x
function (x:xs) (y:ys)
    | x == y = x : function xs ys
    | otherwise = <strong>'-'</strong> : function xs ys