简体   繁体   English

将一种数据类型转换为另一种

[英]convert one data type to another

I am so familiar with imperative language and their features. 我对命令式语言及其功能非常熟悉。 So, I wonder how I can convert one data type to another? 那么,我想知道如何将一种数据类型转换为另一种数据类型?

ex:
   in c++
           static_cast<>
    in c
           ( data_type) <another_data_type>

Use an explicit coercion function. 使用显式强制函数。

For example, fromIntegral will convert any Integral type ( Int , Integer , Word* , etc.) into any numeric type. 例如, fromIntegral会将任何Integral类型( IntIntegerWord*等)转换为任何数字类型。

You can use Hoogle to find the actual function that suits your need by its type. 您可以使用Hoogle按类型查找适合您需要的实际功能。

Haskell's type system is very different and quite a bit smarter then C/C++/Javas. Haskell的类型系统与C / C ++ / Javas非常不同,而且相当聪明。 To understand why you are not going to get the answer you expect, it will help to compare the two. 要理解为什么你不会得到你期望的答案,这将有助于比较两者。

For C and friends the type is a way of describing the layout of data in memory. 对于C和朋友,类型是一种描述内存中数据布局的方式。 The compiler does makes a few sanity checks trying to ensure that memory is not corrupted, but in the end its all bytes and you can call them what ever you want. 编译器确实进行了一些健全性检查,试图确保内存没有损坏,但最后它的所有字节都可以调用它们,无论你想要什么。 This is even more true of pointers which are always laid out the same in memory but can reference anything or (frighteningly) nothing. 对于总是在内存中布局相同但可以引用任何内容或(可怕地)没有引用的指针更是如此。

In Haskell, types are a language that one writes to the compiler with. 在Haskell中,类型是一种用编写器编写的语言。 As a programmer you have no control over how the compiler represents data, and because haskell is lazy a lot of data in your program may be no more then a promise to produce a value on demand (called a thunk in the code GHC's and HUGS). 作为程序员,你无法控制编译器如何表示数据,并且因为haskell是懒惰的,程序中的大量数据可能不再是按需生成值的承诺(在代码GHC和HUGS中称为thunk ) 。 While ac compiler can be instructed to treat data differently, there is no equivalent way to tell a haskell compiler to treat one type like another in general . 虽然可以指示ac编译器以不同方式处理数据 ,但是没有相同的方法来告诉haskell编译器通常将一种类型视为另一种类型。

As mentioned in other answers, there are some types where there are obvious ways to convert one type to another. 正如其他答案中所提到的,有些类型有明显的方法可以将一种类型转换为另一种类型。 Any of the numerical types like Double, Fraction, or Real (in general any instance of the Num class) can be made from an Integer , but we need to use a function specifically designed to make this happen. 任何数值类型,如Double,Fraction或Real(通常是Num类的任何实例)都可以从Integer ,但我们需要使用专门设计的函数来实现这一点。 In a sense this is not a 'cast' but an actual function in the same way that \\x -> x > 0 is a function for converting numbers into booleans. 从某种意义上说,这不是一个'强制转换',而是一个实际的函数,就像\\x -> x > 0是一个将数字转换成布尔值的函数一样。

I'll make one last guess as to why you might be asking a question like this. 我最后会猜到为什么你可能会问这样的问题。 When I was just starting with haskell I wrote a lot of functions like: 当我刚开始使用haskell时,我写了很多函数,比如:

area :: Double -> Double -> Double -- find the area of a rectangle
area x y = x * y

I would then find myself dumping fromInteger calls all over the place to get my data in the correct type for the function. 然后我会发现自己从整个地方转移来自fromInteger调用,以获得正确的函数类型的数据。 Having come from a C background I wrote all my functions with monomorphic types. 来自C背景我用单态类型编写了所有函数。 The trick to not needing to cast from one type to another is to write functions that work with different types. 不需要从一种类型转换为另一种类型的技巧是编写适用于不同类型的函数。 Haskell type classes are a huge shift for OOP programmers and so they often get ignored the first couple of tries, but they are what make the otherwise very strict haskell type system usable. Haskell类型类对于OOP程序员来说是一个巨大的转变,因此它们经常在前几次尝试时被忽略,但它们是使得非常严格的haskell类型系统可用的原因。 If you can relax your type signatures (eg area :: (Num a)=> a -> a -> a ) you will find yourself wishing for that cast function much less often. 如果你可以放松你的类型签名(例如area :: (Num a)=> a -> a -> a )你会发现自己希望这种演员函数不那么频繁。

There are many different functions that convert different data types. 有许多不同的函数可以转换不同的数据类型。 The examples would be: 例子是:

fromIntegral - to convert between say Int and Double fromIntegral - 在Int和Double之间转换

pack / unpack - to convert between ByteString and String pack / unpack - 在ByteString和String之间进行转换

read - to convert from String to Int read - 从String转换为Int

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

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