我是Haskell的初学者.我有一个我在Haskell中使用的元组列表:结构是这样的[(a,b),(c,d),(e,f),(g,h)]
我想要的是根据第二个值返回此元组中的最大元素:因此,如果元组列表是[(4,8),(9,10),(15,16),(10,4)],我想要最大元素(15,16).
但我不知道该怎么做.这是我到目前为止的尝试,
maximum' :: (Ord a) => (Num a) => [(a,b)] -> a
maximum' [] = error "maximum of empty list"
maximum' [(x,y)] = -1
maximum' (x:xs)
| snd x > snd(xs !! maxTail) = 0
| otherwise = maxTail
where maxTail = maximum' xs + 1
Run Code Online (Sandbox Code Playgroud)
我得到这个错误信息对我来说没有意义:
newjo.hs:23:25:
Could not deduce (a ~ Int)
from the context (Ord a, Num a)
bound by the type signature for
maximum' :: (Ord a, Num a) …Run Code Online (Sandbox Code Playgroud) 所以我正在制作一个程序来创建portmanteaus.我有我需要的代码和功能,我把它们放在一起.
这是代码:
def portmanteauscore(start, mid, end):
totallen = len(start) + len(mid) + len(end)
return totallen - abs((len(start)/totallen) - len(start)) - abs((len(mid)/totallen) - len(mid)) - abs((len(end)/totallen) - len(end))
def portmanteaugenerator(word1, word2, words):
mid = longest_common_substring(word1, word2)
start = word1[:word1.index(mid)]
end = word2[len(mid):]
if start + mid in words and mid + end in words:
return start, mid, end
def natalie(words):
"Find the best Portmanteau word formed from any two of the list of words."
wordpermutations = list(itertools.permutations(words))
maxscore, bestnatalie = 0, …Run Code Online (Sandbox Code Playgroud)