'show' returns what I want alongside a weird String

61 Views Asked by At

I have a function that needs to get all of the Integers in a list and display them as a string, I.E "Beans 1.29" should return 129. My function is as follows

multDigitsRecTest :: String -> String
multDigitsRecTest [] = ""
multDigitsRecTest (x:xs)
  | isDigit x = show (digitToInt (x), multDigitsRecTest xs)
  | otherwise = multDigitsRecTest xs

It seems to return the integer with other weird stuff. Is there a way I can just return the int as the string?

1

There are 1 best solutions below

1
On BEST ANSWER

You basically here want to filter the characters that are digits. So you can implement this as:

import Data.Char(isDigit)

multDigitsRecTest :: String -> String
multDigitsRecTest = filter isDigit

For example:

Prelude Data.Char> multDigitsRecTest "Beans 1.29"
"129"

The reason it is printing noise, is because you call show (digitToInt x, multDigitsRecTest xs). This is a 2-tuple (Int, String). Since both Int and String are members of the Show typeclass. The tuple is as well. It will thus print (2, "(5, \"\")") for example.