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

Categories

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.


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

1 Answer

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...