加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 对Haskell的简要介绍:“……没有一种类型包含2和’b’

发布时间:2020-12-16 18:20:35 所属栏目:安全 来源:网络整理
导读:我目前正在学习 Haskell,所以这里有一个初学者的问题: 单词类型在下面的文字中是什么意思? 单一类型是一个特殊的Haskell术语吗?这是否意味着原子类型? 或者这是否意味着我永远不能在Haskell中创建一个列表,我可以将1和’c’放在一起? 我在想一个类型是
我目前正在学习 Haskell,所以这里有一个初学者的问题:

单词类型在下面的文字中是什么意思?

单一类型是一个特殊的Haskell术语吗?这是否意味着原子类型?

或者这是否意味着我永远不能在Haskell中创建一个列表,我可以将1和’c’放在一起?

我在想一个类型是一组值.

所以我无法定义包含Chars和Ints的类型?

那么代数数据类型呢?

类似于:data IntOrChar = In Int | Ch Char? (我想这应该有用,但我很困惑作者对那句话的意思.)

顺便说一下,这是在Haskell中制作列表的唯一方法,我可以将Ints和Chars放在一起吗?还是有更棘手的方法?

Scala类比:在Scala中,可以将隐式转换写入表示Ints和Chars(如IntOrChar)的类型,然后可以将Int和Chars放入List [IntOrChar],这是不可能的哈斯克尔?如果我想将它们放入IntOrChar列表中,我是否总是必须将每个Int或Char显式地包装到IntOrChar中?

从Gentle Intro to Haskell开始:

Haskell also incorporates polymorphic types—types that are
universally quantified in some way over all types. Polymorphic type
expressions essentially describe families of types. For example,
(forall a)[a] is the family of types consisting of,for every type a,
the type of lists of a. Lists of integers (e.g. [1,2,3]),lists of
characters ([‘a’,’b’,’c’]),even lists of lists of integers,etc.,are
all members of this family. (Note,however,that [2,’b’] is not a
valid example,since there is no single type that contains both 2 and
‘b’.)

解决方法

简短的回答.

在Haskell中没有隐式转换.也没有联合类型 – 只有不相交的联合(代数数据类型).所以你只能写:

someList :: [IntOrChar]
someList = [In 1,Ch 'c']

更长,肯定不温和的答案.

注意:这是一种很少使用的技术.如果您需要它,您可能会过度复杂化API.

然而存在存在的类型.

{-# LANGUAGE ExistentialQuantification,RankNTypes #-}
class IntOrChar a where
   intOrChar :: a -> Either Int Char

instance IntOrChar Int where
   intOrChar = Left

instance IntOrChar Char where
   intOrChar = Right 

data List = Nil
          | forall a. (IntOrChar a) => Cons a List

someList :: List
someList = (1 :: Int) `Cons` ('c' `Cons` Nil)

在这里,我创建了一个只有函数intOrChar的类型类IntOrChar.这样你就可以转换任何类型的forall a. (IntOrChar a)=> a to Int Char.

还有一种在其第二个构造函数中使用存在类型的特殊列表.
这里类型变量a在构造函数范围内绑定(使用forall).所以每一次
你使用Cons,你可以传递任何类型的forall a. (IntOrChar a)=> a作为第一个论点.因此,在破坏(即模式匹配)期间,第一个参数将会出现
仍然是forall a. (IntOrChar a)=>一个.你可以用它做的唯一事情是传递它或调用它上面的intOrChar并将其转换为Either Int Char.

withHead :: (forall a. (IntOrChar a) => a -> b) -> List -> Maybe b
withHead f Nil = Nothing
withHead f (Cons x _) = Just (f x)

intOrCharToString :: (IntOrChar a) => a -> String
intOrCharToString x = 
   case intOrChar of
    Left i -> show i
    Right c -> show c

someListHeadString :: Maybe String
someListHeadString = withHead intOrCharToString someList

再说一遍,你不能写

{- Wont compile
safeHead :: IntOrChar a => List -> Maybe a
safeHead Nil = Nothing
safeHead (Cons x _) = Just x
-}

-- This will
safeHead2 :: List -> Maybe (Either Int Char)
safeHead2 Nil = Nothing
safeHead2 (Cons x _) = Just (intOrChar x)

safeHead将无法工作,因为你想要一种IntOrChar a =>也许a在safeHead范围内绑定并且Just x将具有IntOrChar类型a1 =>也许a1与Cons范围内的a1绑定.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读