program.exe
program.exe

Reputation: 432

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']

Upvotes: 0

Views: 121

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

x' is a variable that represents the Chars in the String.

You can see this list comprehension as something similar in Python:

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

It will thus enumerate over the Chars in the string, and check if the Char matches with the query x, if that holds it will add that to the list.

In Python however, an identifier can not contain a quote. In haskell that is often used to mimic the prime symbol [wiki] to denote that something is similar to the item without the prime. It thus means that x' is somehow a bit similar to x.

Upvotes: 2

Related Questions