首页 > 解决方案 > 如何检查列表的最后一个元素是否是另一个列表的成员?

问题描述

我在列表方面遇到了一些问题。

我有以下列表:

sentence = [['t','h','e'], ['b','o','y'],['i','s'],['h','a','p','p','y']]

shortVowel = [['a'], ['e'], ['y']]

我需要做一个 if 语句来检查两件事:

(1) 如果元素 x 是最后一个“子列表”的成员(当然,它是主列表的最后一个元素),

(2) 如果列表的元素(子列表)是最后一个子列表shortVowel的最后一个成员。

(1)我能够做到elem 'а' (last sentence))(检查'a'是否是 的成员['h','a','p','p','y'],它是。

(2)是我不知道如何获得的。

例如,最后一个子列表的最后一个元素sentencey(来自happY)。并且y也是列表中的子shortVowel列表。

我需要检查一下。

我尝试了一些东西,比如elem shortVowel (last sentence),但没有奏效。

这是我当前的代码:

import Data.List

sentence = [['t','h','e'], ['b','o','y'],['i','s'],['h','a','p','p','y']]

shortVowel = [['a'], ['e'], ['y']]

main =  if ((elem 'а' (last sentence)) || (elem 'о' (last sentence)) || (elem 'у' (last sentence)) && (elem shortVowel (last sentence)))
           then putStrLn "A"
        else if (elem 'р' (last sentence))
           then putStrLn "B"
        else putStrLn "C"

标签: haskell

解决方案


shortVowel = [['a'], ['e'], ['y']]我会写而不是写shortVowel' = ['a', 'e', 'y']。这与shortVowel' = "aey". 现在做

or [x == last $ last sentence | x <- shortVowel']

以上检查是否有任何元素shortVowel是句子中最后一个单词的最后一个元素。如果你想检查所有短元音然后做

[(x, x == last $ last sentence) | x <- shortVowel']

推荐阅读