相关疑难解决方法(0)

Haskell中的所有内容都存储在thunk中,即使是简单的值吗?

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中的表示方式.

haskell lazy-evaluation thunk

41
推荐指数
4
解决办法
2923
查看次数

Swift:guard let 和 where - 优先级

有时,我想guardlet&结合使用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 …

swift

2
推荐指数
1
解决办法
1725
查看次数

标签 统计

haskell ×1

lazy-evaluation ×1

swift ×1

thunk ×1