我有以下代码:
import Text.JSON
-- get the value in a JSON object that has this key
getByKey :: JSValue -> String -> Maybe JSValue
getByKey object key =
case object of
JSObject a ->
getFirst keyValues keyMatches
where keyValues = fromJSObject object
keyMatches (key', value) = key == key'
_ -> Nothing
-- get the first item in the list matching a predicate
getFirst :: [a] -> (a -> Bool) -> Maybe a
getFirst [] pred = Nothing
getFirst (x:xs) pred …Run Code Online (Sandbox Code Playgroud) 我对OCaml中的关联性感到非常困惑.
我们先来看一些例子.
1. +左联或右吗?
我认为它是联想的.这意味着如果我们拥有a + b + c + d,那就是((a+b)+c)+d.
但是如果我们这样做f1 1 + f2 2呢?它会抛出错误吗?因为它应该是((f1 1)+f2) 2,对吧?
2. ::是正确的联想,但如果与之一起使用会,怎样?
如果我们这样做4,3::[],那么我们就有了[4,3].它会创建一个元组(4,3),然后::进入[].
那为什么不起作用4,3::5,6::[]?
功能
说我们有let f x y = y x 5,所以y是一个带两个参数的函数.
如果我们这样做f 1 + 2,因为+实际上是一个带两个参数的函数,为什么f 1 + 2不起作用?为什么不+成为参数f?
正确的联想
如何创建具有正确关联性的中缀函数? …
我有一个简单的汇编程序,它试图通过在内存中存储临时变量来返回3:
.text
.global _start
_start:
movl $2, %ebx
mov %ebx, -0x4(%ebp)
movl $1, %ebx
add -0x4(%ebp), %ebx
movl $1, %eax
int $0x80
Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,这会给我一个分段错误:
$ as out.s -o out.o
$ ld -s -o out out.o
$ ./out
segmentation fault
Run Code Online (Sandbox Code Playgroud)
我想这是因为我从不初始化%ebp.如果我只使用寄存器而不访问相关的主内存,我的程序运行正常%ebp.
应该初始化到什么价值?程序malloc在启动时应该是自己的堆栈吗?
我有以下32位x86汇编代码:
.text
.global _start
_start:
/* Compare 3 < 2 */
mov $2, %eax
cmp $3, %eax
/* Set the low byte of %eax according to the SF and 0F flags. */
setl %al
/* Syscall exit with the value of %eax. */
mov %eax, %ebx
mov $1, %eax
int $0x80
Run Code Online (Sandbox Code Playgroud)
如果我汇编并链接它,我会得到一个以退出代码1退出的二进制文件.如果我切换CMP操作数,我的二进制文件将退出0.
这与我的预期相反.CMP 根据x86参考:
通过从第一个操作数中减去第二个操作数,然后以与SUB指令相同的方式设置状态标志来执行比较.
第一个操作数是3,第二个操作数是2. 3-2是1,大于零,那么为什么SF(符号标志)设置?