Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell中链表的基本操作_Haskell_Linked List - Fatal编程技术网

Haskell中链表的基本操作

Haskell中链表的基本操作,haskell,linked-list,Haskell,Linked List,我试图在Haskell中为链表编写一些基本的列表操作,但是我可以用手来完成这个操作。我想将一个列表附加到另一个列表,但它根本不起作用,以下是我的代码: data ML a = E | L a (ML a) deriving (Show) myList = (L 1 (L 2 (L 3 (L 4 E)))) myHead :: ML a -> a myHead E = error "empty list" myHead (L a _) = a myTail :: ML a ->

我试图在Haskell中为链表编写一些基本的列表操作,但是我可以用手来完成这个操作。我想将一个列表附加到另一个列表,但它根本不起作用,以下是我的代码:

data ML a = E | L a (ML a) deriving (Show)

myList = (L 1 (L 2 (L 3 (L 4 E))))

myHead :: ML a -> a
myHead E = error "empty list"
myHead (L a _) = a

myTail :: ML a -> ML a
myTail E = error "empty list"
myTail (L _ a) = a

myAppend :: ML a -> ML a -> ML a
myAppend E a = a
myAppend (x L xs) ys = x L (xs myAppend ys)
我还问了一个问题,如何将此列表显示为类似“1,2,3”的字符串。我得到了一个函数,它显示为一个类似于[1,2,3]的列表,但不是一个字符串

toString :: ML a -> [a]
toString E = []
toString (L a l) = a:(toString l)

我认为基本问题在最后一行:

myAppend :: ML a -> ML a -> ML a
myAppend E a = a
myAppend (x L xs) ys = x L (xs myAppend ys)
现在应该可以了

关于您的第二个问题:
tostringmla->[a]
。实际上,
[a]
是一个列表,而不是一个
字符串(嗯,
字符串
是一个
字符的列表:
类型字符串=[Char]
),猜猜看:Haskell中的列表是一个链接列表。这意味着您的
toString::mla->[a]
实际上更像是一个
toList

如果您想以
字符串
的形式编写内容,这意味着元素应该具有文本表示形式。您可以使用类型约束来编写它,比如
Show a=>
。现在签名是:
toString::Show a=>mla->[String]

我们可以将
a
s的列表转换为文本表示,方法是
map
ping所有元素为它们的文本表示,并
插入::[a]->[[a]]->[a]
,例如使用逗号,如:

import Data.List(intercalate)

toList :: ML a -> [a]
toList E = []
toList (L x xs) = x : toList xs

toString :: Show a => ML a -> String
toString = intercalate "," . map show . toList
或者我们可以“自己实现功能”:


最后一行应该是
myAppend(lxxs)ys=lx(myAppend-xs-ys)
。您的toString会将列表转换为标准的Haskell列表,而不是字符串。您可以将函数或数据构造函数用作中缀运算符,但需要将其包装为倒勾,例如:
x`L`(xs`myAppend`ys)
。append函数可以正常工作,但toString仍然无法工作。有没有一种简单的方法可以将整型列表转换成字符串?@Vajk:是的,你必须导入
插入
函数first@Vajk:导入数据的复杂性是什么?List(interlate)
?@Vajk:显然还有另一种方法,因为Haskell中也实现了
interlate
。将更新答案。您不能将两个内容与
(x y)
一起添加。您可以使用
(x+y)
来实现这一点。
(+)
运算符是
Num
类型类的函数。所以签名应该是:
myAdd::Num a=>mla->mla->mla
import Data.List(intercalate)

toList :: ML a -> [a]
toList E = []
toList (L x xs) = x : toList xs

toString :: Show a => ML a -> String
toString = intercalate "," . map show . toList
toString :: Show a => ML a -> String
toString E = ""
toString (L x E) = show x
toString (L x xs) = show x++',' : toString xs