如何将变量转换为字符串?

时间:2022-10-30 11:52:18

For example so that it works like this toString (Var x)= "x"

例如,它就像这样工作toString(Var x)=“x”

2 个解决方案

#1


1  

Use the show function:

使用show功能:

putStrLn (show x)

will print out the "x" variable. (Naturally, you don't need to use it with putStrLn, either -- show returns a string that can be used anywhere like a string.)

将打印出“x”变量。 (当然,你不需要将它与putStrLn一起使用, - show返回一个字符串,可以像字符串一样在任何地方使用。)

#2


0  

If I understand you correctly, you're asking how to convert programming constructs into strings. You aren't concerned with what 'x' represents so much as you are that the programmer called it "x" in the source file.

如果我理解正确,你就会问如何将编程结构转换为字符串。您并不关心'x'代表什么,因为程序员在源文件中将其称为“x”。

You can convert data constructors into strings using some of the Scrap Your Boilerplate components. Here's an example that does just what you asked.

您可以使用Scrap Your Boilerplate组件将数据构造函数转换为字符串。这是一个例子,可以满足您的要求。

{-# LANGUAGE DeriveDataTypeable #-}

module Main where

import Data.Data

data Var a = Var a
data X = X deriving (Data, Typeable)

toString :: Data a => Var a -> String
toString (Var c) = show (toConstr c)

main :: IO ()
main = putStrLn $ "toString (Var x)= " ++ show (toString (Var X))

output:

$ ghci Test.hs
GHCi, version 6.10.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( Test.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
toString (Var X)= "X"
*Main>

For a real example, I suggest looking at the RJson library.

举个实例,我建议看一下RJson库。

#1


1  

Use the show function:

使用show功能:

putStrLn (show x)

will print out the "x" variable. (Naturally, you don't need to use it with putStrLn, either -- show returns a string that can be used anywhere like a string.)

将打印出“x”变量。 (当然,你不需要将它与putStrLn一起使用, - show返回一个字符串,可以像字符串一样在任何地方使用。)

#2


0  

If I understand you correctly, you're asking how to convert programming constructs into strings. You aren't concerned with what 'x' represents so much as you are that the programmer called it "x" in the source file.

如果我理解正确,你就会问如何将编程结构转换为字符串。您并不关心'x'代表什么,因为程序员在源文件中将其称为“x”。

You can convert data constructors into strings using some of the Scrap Your Boilerplate components. Here's an example that does just what you asked.

您可以使用Scrap Your Boilerplate组件将数据构造函数转换为字符串。这是一个例子,可以满足您的要求。

{-# LANGUAGE DeriveDataTypeable #-}

module Main where

import Data.Data

data Var a = Var a
data X = X deriving (Data, Typeable)

toString :: Data a => Var a -> String
toString (Var c) = show (toConstr c)

main :: IO ()
main = putStrLn $ "toString (Var x)= " ++ show (toString (Var X))

output:

$ ghci Test.hs
GHCi, version 6.10.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( Test.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
toString (Var X)= "X"
*Main>

For a real example, I suggest looking at the RJson library.

举个实例,我建议看一下RJson库。