首页 > 解决方案 > 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']

标签: haskell

解决方案


x'是一个变量,表示 中的Chars String

您可以在 Python 中将此列表推导视为类似的内容:

len([y for y in xs if x == y])

因此,它将枚举Char字符串中的 s,并检查是否Char与 query 匹配,如果匹配x,则将其添加到列表中。

然而,在 Python 中,标识符不能包含引号。在 haskell 中,通常用于模仿素数符号[wiki]来表示某物与没有素数的项目相似。因此,这意味着x'有点类似于x.


推荐阅读