Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
778 views
in Technique[技术] by (71.8m points)

pattern matching - How to compare a character to a number in Haskell?

I'm trying to do it by pattern matching with the following

[x| x <- "example string", x > 106]

I know you can compare things such as x > a, and I'm guessing an explicit conversion is needed. One method to do this I found online but doesn't work is

[x| x <- "example string", ord x > 106]

And apparently doesn't recognise ord as a valid keyword.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

ord :: Char -> Int is not a keyword, it is a function from the Data.Char module:

import Data.Char(ord)

myExpr = [x | x <- "example string", ord x > 106]

Here it might be more convenient to work with filter:

import Data.Char(ord)

myExpr = filter ((106 <) . ord) "example string"

both return:

Prelude Data.Char> [x | x <- "example string", ord x > 106]
"xmplstrn"
Prelude Data.Char> filter ((106 <) . ord) "example string"
"xmplstrn"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...