haskell - Can someone explain this haskell function to count occurences of char in a string
问题描述
I am mostly unsure about what x' represents here. Any explanations would be appreciated.
count :: Char -> String -> Int
count x xs = length [x'|x'<-xs, x==x']
解决方案
x'
是一个变量,表示 中的Char
s String
。
您可以在 Python 中将此列表推导视为类似的内容:
len([y for y in xs if x == y])
因此,它将枚举Char
字符串中的 s,并检查是否Char
与 query 匹配,如果匹配x
,则将其添加到列表中。
然而,在 Python 中,标识符不能包含引号。在 haskell 中,通常用于模仿素数符号[wiki]来表示某物与没有素数的项目相似。因此,这意味着x'
有点类似于x
.
推荐阅读
- sql - 有谁知道如何将年份值插入日期以使日期和月份保持不变
- javascript - 向 Figma 插件中没有空数组的节点添加填充(TypeError:值没有属性)
- linux - 是否可以在 Linux 中为 Windows 构建 React-Native 应用程序?
- javascript - 在谷歌表格中创建一个按钮以将单元格值传输到它旁边的单元格
- javascript - D3 径向树自定义编辑圆 cytoscape.js-cise-master
- angular - 角度材料日期选择器中带有asp.net核心问题的角度8
- python - 函数可以是 Python 部分中的 kwarg 之一吗?
- docker - 无法在 openshift 中创建文件导致权限被拒绝
- javascript - 我有一个错误,但我没有找到问题
- javascript - 如何从 vue 对话框/模态中获取用户输入