Monday, 6 June 2016

Haskell: Get all digits in given string

Write a function, which takes a string (it contains characters, numbers, special symbols etc.,) as input and return all the numbers in the string.

getNumbers list = [x | x <- list, isDigit x]

Above snippet extract all the numbers in given string.
Prelude> import Data.Char
Prelude Data.Char> 
Prelude Data.Char> let getNumbers list = [x | x <- list, isDigit x]
Prelude Data.Char> 
Prelude Data.Char> getNumbers "12HAbsja123nkasn8382"
"121238382"


Previous                                                 Next                                                 Home

No comments:

Post a Comment