gpt4 book ai didi

list - Haskell:字符串和/或[字符串]的异构列表的类型(没有样板)?

转载 作者:行者123 更新时间:2023-12-01 22:29:32 24 4
gpt4 key购买 nike

我想要一个 String[String] 的异构列表,如下所示:

strs = ["h", ["x", "y"], "i", ["m", "n", "p"]]

我知道我可以使用自定义数据类型来做到这一点:

data EitherOr t = StringS t | StringL [t]

eitherOrstrs :: [EitherOr String]
eitherOrstrs = [StringS "h", StringL ["x", "y"], StringS "i", StringL ["m", "n", "p"]]

但我很好奇这是否可以在没有任何样板的情况下实现,如上面的 strs 所示。

到目前为止我已经尝试过:

{-# LANGUAGE ExistentialQuantification #-}

class Listable a where
toListForm :: [String]

instance Listable String where
toListForm s = [s]

instance Listable [String] where
toListForm = id

strs :: forall a. Listable a => [a]
strs = ["h", ["x", "y"], "i", ["m", "n", "p"]]

但还没有找到有效的方法:

The class method ‘toListForm’
mentions none of the type or kind variables of the class ‘Listable a’
When checking the class method: toListForm :: [String]
In the class declaration for ‘Listable’

有人知道这是否可行吗?

最佳答案

这将适用于带有一些扩展技巧的任意嵌套字符串列表:

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLists #-}

import GHC.Exts
import Data.String

data MyStringListThingy
= String String
| List [MyStringListThingy]
deriving (Eq, Show)

instance IsString MyStringListThingy where
fromString = String

instance IsList MyStringListThingy where
type Item MyStringListThingy = MyStringListThingy
fromList = List
fromListN _ = List
toList (String s) = [String s]
toList (List ss) = ss

strs :: MyStringListThingy
strs = ["h", ["x", "y"], "i", ["m", "n", "p", ["q", ["r", "s"]]]]

尽管如此,您至少需要 GHC 7.8,可能是 7.10(我还没有测试过 7.8)。

如果没有样板文件,这并不能完全摆脱,编译器将隐式函数调用放在每个这些文字的前面:

strs = fL [fS "h", fL [fS "x", fS "y"], fS "i", fL [fS "m", fS "n", fS "p", fL [fS "q", fL [fS "r", fS "s"]]]]
where
fL = fromList
fS = fromString

虽然没有 fLfS 别名,但我只是这样做了,这样我就不必输入那么多了。它只是感觉没有样板文件,因为编译器为您放置了这些函数调用,但您仍然需要将这些值转换为 MyStringListThingy

您也可以使用这个技巧摆脱异构数字列表,因为数字文字也是多态的,这就是 OverloadedStringsOverloadedLists 扩展所做的那些文字也是如此。通过制作一个包装列表和字符串的类型,然后实例化您允许 Haskell 从这些文字转换为您的自定义类型的必要类型类。 TypeFamilies 扩展仅对 IsList 实例是必需的。如果您想在 GHCi 中使用它,您还必须在那里启用所有这些扩展,但它确实有效。

一个更通用的实现是

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLists #-}

import GHC.Exts
import Data.String

data NestedList a
= Item a
| List [NestedList a]
deriving (Eq, Show)

instance IsList (NestedList a) where
type Item (NestedList a) = NestedList a
fromList = List
fromListN _ = List
toList (List xs) = xs
toList item = [item]

instance IsString (NestedList String) where
fromString = Item

instance Num a => Num (NestedList a) where
fromInteger = Item . fromInteger

Num 实例并没有实现它需要的一切,只是足以展示它的工作原理

> [1, [2, 3]] :: NestedList Int
List [Item 1, List [Item 2, Item 3]]

不过,我不建议在实际代码中使用它。

关于list - Haskell:字符串和/或[字符串]的异构列表的类型(没有样板)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30216149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com