- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个自定义列表类型:
data NNList a = Sing a | Append ( NNList a) ( NNList a) deriving (Eq)
data CList a = Nil | NotNil ( NNList a) deriving (Eq)
cListGet :: CList a -> Maybe (a, CList a)
cListGet :: CList a -> Maybe (a, CList a)
cListGet Nil = Nothing
cListGet xs@(NotNil nxs) =
case nxs of
Sing x -> (x, Nil)
Append l r -> ((fst $ cListGet (NotNil l)), (Append (snd $ cListGet (NotNil l)), r))
最佳答案
好吧,人们通常会将您拥有的数据结构称为一种树,而不是列表。但无论如何...
问题 #1:Haskell 对缩进敏感,您的 case
表达式没有缩进。这会导致解析错误。
问题#2,以及更大的问题:你还没有理解 Maybe
类型工作。我的印象是你认为它在更常见的语言中像空值一样工作,这让你失望。
在像 Java 这样的语言中,null
是一个可以在大多数其他值可以出现的地方出现的值。如果我们有一个具有以下签名的方法:
public Foo makeAFoo(Bar someBar)
// Way #1: pass in an actual value
Bar theBar = getMeABar();
Foo result = makeAFoo(theBar);
// Way #2: pass in a null
Foo result2 = makeAFoo(null)
theBar
和
null
在某种意义上是“并行的”,或者更准确地说,它们具有相同的类型——您可以在程序中用另一个替换一个,并且在两种情况下都会编译。
"hello"
和
Nothing
没有相同的类型,你不能在另一个去的地方使用一个。 Haskell 区分了这三件事:
"hello" :: String
Nothing :: Maybe String
Just "hello" :: Maybe String
Maybe a
,如果您确实有值,则必须使用
Just
,它就像一个包装器,表示“这不仅仅是一个
a
,它还是一个
Maybe a
。”
Just
是
case
的右手边表达式,我们可以这样修复:
-- This still fails to compile!
cListGet :: CList a -> Maybe (a, CList a)
cListGet Nil = Nothing
cListGet xs@(NotNil nxs) =
case nxs of
-- I added 'Just' here and in the next line:
Sing x -> Just (x, Nil)
Append l r -> Just (fst $ cListGet (NotNil l), (Append (snd $ cListGet (NotNil l)), r))
fst $ cListGet (NotNil l)
,遇到相反的问题:
cListGet
返回
Maybe (a, CList a)
,但是
fst
工作于
(a, b)
,不在
Maybe (a, b)
.您需要对
cListGet
的结果进行模式匹配测试是否是
Nothing
或
Just (x, l')
. (同样的问题也发生在您的
snd $ cListGet (NotNil l)
中。)
Append
构造函数错误。您以
(Append foo, bar)
的形式获得它, 在
foo
之间不应该有逗号和
bar
.在 Haskell 中,与大多数其他语言相比,这种事情会给您带来更多令人困惑的错误消息,因为当 Haskell 看到这一点时,它不会告诉您“您犯了一个语法错误”; Haskell 比大多数语言更直接,因此它认为您正在尝试与
Append foo
配对。作为第一个元素,和
bar
作为第二个,所以它的结论是
(Append foo, bar)
必须有类型
(NNList a -> NNList a, NNList a)
.
CList a
的“头”和“尾” .这意味着什么?在 Haskell 的情况下
[a]
类型,带构造函数
[]
和
:
,这很明显:头部是
x
在
x:xs
,尾部是
xs
.
cListHead :: CList a -> Maybe a
cListHead Nil = Nothing
-- No need to cram everything together into one definition; deal with
-- the NNList case in an auxiliary function, it's easier...
cListGet (NotNil nxs) = Just (nnListHead nxs)
-- Note how much easier this function is to write, because since 'NNList'
-- doesn't have a 'Nil' case, there's no need to mess around with 'Maybe'
-- here. Basically, by splitting the problem into two functions, only
-- 'cListHead' needs to care about 'Maybe' and 'Just'.
nnListHead :: NNList a -> a
nnListHead (Sing a) = a
nnListHead (Append l _) = nnListHead l
CList
的子部分。或
NNList
.拿这个例子:
example :: CList Int
example = NotNil (Append (Append (Sing 1) (Sing 2)) (Sing 3))
1
.但是
example
中定义的结构没有子部分包含
2
和
3
不含
1
以及。您必须构建一个新的
CList
使用与原始形状不同的形状来获得它。这是可能的,但坦率地说,我不认为它作为初学者练习的值(value)。
NotNil
|
v
Append
/ \
v v
Sing Append
| / \
v v v
1 Sing Sing
| |
v v
2 3
关于list - 在 Haskell 中获取自定义列表类型的头部和尾部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15651772/
我发现了一些令人费解的行为。给定一个包含 50 个日期的列表: structure(c("15513", "12830", "16503", "-3628", "15833", "13553", "4
我正在尝试创建单链表,但我不知道我做错了什么。 在插入元素 5、6、7、2、3、4 后,尾部应该是 4,但我得到的是 3,我不明白为什么。 这是我的代码: public void Insert(int
这是我的尾部代码(前 10 行): #include #include #include typedef char storage_datatype; #define MAXLINESIZE 1
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
是否有一种有效的方法可以从 List 中删除 X 元素的范围(例如尾部),例如LinkedList 在 Java 中? 显然可以一个一个地删除最后一个元素,这应该会导致 O(X) 级别的性能。至少对于
在之前的一篇文章我们介绍了《如何向php数组中头部和尾部添加元素》既然有添加元素,那么就有删除元素,今天这篇文章详细介绍如何删除数组中的头部元素和尾部元素,还有任意数组元素。 删除末尾元素:arr
在双向链表的实现中,我使用了典型的结构: struct node { void *data; struct node *prev; struct node *next; };
ECMA-335,III.2.4指定可以在递归函数中使用的tail.前缀。但是,我在C#和F#代码中都找不到它的用法。有使用in的示例吗? 最佳答案 您不会在当前的MS C#编译器生成的任何代码中找到
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
这个问题在这里已经有了答案: How to 'grep' a continuous stream? (13 个答案) 关闭 8 年前。 Tail 有以下选项: -f The -f opti
我试图弄清楚一旦我击中某个字符,如何从尾部修剪 Bash 中的字符串。 示例:如果我的字符串是这个(或任何链接):https://www.cnpp.usda.gov/Innovations/DataS
我试图弄清楚一旦我击中某个字符,如何从尾部修剪 Bash 中的字符串。 示例:如果我的字符串是这个(或任何链接):https://www.cnpp.usda.gov/Innovations/DataS
当我执行这段代码时,循环总是在第一次结束(即使 auth.log 的最后两行不包含“exit”),这意味着 $c总是得到一些字符串: while true; do c=$(tail -2 /v
我正在尝试编写一个“添加”函数,该函数接受节点中保存的值(表示为“n”),以及节点要添加到链表中的位置(表示为“pos”)。 我看到代码中有 3 个单独的添加函数 - addAtBeginning、a
为什么我不能得到“cd fjadf”? 程序总是向我显示 Bus error: 10... 我想用这个super_cut_tail()函数来截断用户指定的///fjdakf。但是为什么这个功能不能实现
有一个简化的表 mytable,其中列 ('id', 'mycolumn') 为 int 和 varchar(255 )分别。 在 mycolumn 中查找当前字符串具有最长公共(public)右侧部
这个问题已经有答案了: 已关闭13 年前。 Possible Duplicate: Get last n lines of a file with Python, similar to tail 你好
我想通过对写出的响应主体进行哈希处理来计算响应的实体标签。当我计算实体标签时,将实体标签添加到响应 header 已经太晚了。我想将实体标签添加到预告片中。我看到 net/http 包支持编写预告片,
我正在尝试通过 script.sh 从第 2 行到第 5 行打印文件 (myfile) 的内容。脚本无法从位置 2 打开文件。并且内容从第 1 行打印到第 4 行。以下是文件内容、命令和命令的输出。
在一个特殊的控制台上,我喜欢从/var/log/syslog 中过滤一些信息。这并不是很棘手: tail -f /var/log/syslog | awk '{print $2,$1,$9,$3,"\
我是一名优秀的程序员,十分优秀!