Haskell堆中以下值/表达式/函数的thunk是什么样的?
val = 5 -- is `val` a pointer to a box containing 5?
add x y = x + y
result = add 2 val
main = print $ result
Run Code Online (Sandbox Code Playgroud)
考虑到它的惰性评估模式,可以很好地了解这些在Haskell中的表示方式.
有时,我想guard与let&结合使用where来简化我的代码。但我想知道 let 的优先级是什么以及在哪里。例如:
class Person {
func check() -> Bool? {
print("checking")
return nil
}
}
func test(dont: Bool) {
let person = Person()
guard let check = person.check() where dont else {
print("should not check")
return
}
print("result: \(check)")
}
test(false)
Run Code Online (Sandbox Code Playgroud)
正如您所看到的控制台结果,打印输出为:
let check = person.check() where dont对于in语法的条件guard <condition> else { },即使 in 中的表达式where与 in 中表达式的结果无关let,Swift 似乎也是let先执行后检查where。有时在我的代码中,let可选绑定需要大量计算,并且where …