简体   繁体   English

'show'返回我想要的奇怪的字符串

[英]'show' returns what I want alongside a weird String

I have a function that needs to get all of the Integers in a list and display them as a string, IE "Beans 1.29" should return 129. My function is as follows 我有一个函数需要获取列表中的所有整数并将它们显示为字符串,IE“Beans 1.29”应该返回129.我的函数如下

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? 有没有办法可以将int作为字符串返回?

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) . 它是打印噪音的原因,是因为你调用show (digitToInt x, multDigitsRecTest xs) This is a 2-tuple (Int, String) . 这是一个2元组(Int, String) Since both Int and String are members of the Show typeclass. 因为IntString都是Show类型类的成员。 The tuple is as well. 元组也是如此。 It will thus print (2, "(5, \\"\\")") for example. 因此它将打印(2, "(5, \\"\\")")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM