haskell - 如何检查列表的最后一个元素是否是另一个列表的成员?
问题描述
我在列表方面遇到了一些问题。
我有以下列表:
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)是我不知道如何获得的。
例如,最后一个子列表的最后一个元素sentence
是y
(来自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"
解决方案
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']
推荐阅读
- python - 为什么要删除 __init__.py 中的模块?
- r - 如何在 base r 中制作漂亮的表格(不是降价)
- ruby - 安装 gem 后找不到 pry 命令
- php - 邮递员查询到查询另一个页面的 API 端点 - 可能在第二页上有 $_SESSION 变量?
- reactjs - 如何在可以从应用程序访问的 React 应用程序 URL 中存储变量
- javascript - 为什么忽略表格指定的水平尺寸?
- vue.js - Vuetify 渲染标签而不是类名
- kivy - 无法使用 Buildozer 编译 APK 版本
- elasticsearch - 将 filebeat 日志发送到 logstash 以使用 docker 元数据进行索引
- python - 任何人都可以帮助我了解这部分 python 代码的含义吗?