我想在Java或Haskell中实现垃圾收集器(GC),但这有意义吗?
我是否能够控制我自己的GC实现何时启动而不是GC的实现语言?
我想这个问题可以有三种答案:
看看这些:
这与我在带有垃圾收集器的语言上构建解释器并不重复.我需要一个翻译垃圾收集器?因为我不希望我的引导与解释底层GC.
java compiler-construction interpreter garbage-collection vm-implementation
是否有可能检查控制权交给finally来自try或其中一个catch?就像是:
try {
// Some code
} catch(Exception e) {
// Some more code
} finally {
// Check if control came from try or from catch?
}
Run Code Online (Sandbox Code Playgroud) 惰性是《纯函数式数据结构》一书中的基石,但没有明确描述他是如何获得它的,至少对我来说是这样。我以为我只需要写:
datatype 'a susp = $ of 'a
fun force ($x) = x
fun plus (x, y) = $case (x, y) of ($m, $n) => m + n
Run Code Online (Sandbox Code Playgroud)
但后来我收到错误:
- use "ch4.sml";;
[opening ch4.sml]
ch4.sml:3.20 Error: syntax error: inserting ORELSE
[unexpected exception: Compile]
uncaught exception Compile [Compile: "syntax error"]
raised at: ../compiler/Parse/main/smlfile.sml:19.24-19.46
../compiler/TopLevel/interact/evalloop.sml:45.54
../compiler/TopLevel/interact/evalloop.sml:306.20-306.23
../compiler/TopLevel/interact/interact.sml:65.13-65.16
Run Code Online (Sandbox Code Playgroud)
我尝试将函数修改为
fun plus (x, y) = $(print "Evaluating...\n"; force x + force y)
但是用它来调用它并plus ($4, $5)对其进行了评估并且没有记住它,因为它返回$ 9而不是并且$ plus(force $4, force …
我正在重构一些代码,作为中间步骤,我想迭代列表X并将每个元素类型转换为Y. 以下作品:
val xs: List<X>
for (x in xs) {
val y = x as Y
}
Run Code Online (Sandbox Code Playgroud)
但我想知道如何将迭代和类型转换结合起来,以便 1)我不必引入变量x,2)我可以将两行合并为一行。
我尝试过以下方法
val xs: List<X>
for ((y as Y) in xs) {
}
val xs: List<X>
for ((y in xs) as Y) {
}
Run Code Online (Sandbox Code Playgroud)
没有任何成功。是否有可能将类型转换和迭代结合起来?如何?
在Kotlin中,我们可以使用when模式对给定值进行匹配,例如,
when(value) {
1 -> "One"
2, 3 -> "Two or three"
else -> "The rest"
}
Run Code Online (Sandbox Code Playgroud)
通过将两个值嵌套在中,我们还可以同时对多个值进行模式匹配Pair。
when(Pair(value1, value2)) {
(1, "One") -> "One"
(2, "Two"), (3, "Three") -> "Two or three"
else -> "The rest"
}
Run Code Online (Sandbox Code Playgroud)
现在,我想知道如何使用通配符同时对多个值进行模式匹配。我尝试了以下两种方法,但均未成功:
when(Pair(value1, value2)) {
(1, _), (_, "One") -> "One"
(2, _), (_, "Two"), (3, _), (_, "Three") -> "Two or three"
else -> "The rest"
}
Run Code Online (Sandbox Code Playgroud)
和
when(Pair(value1, value2)) {
(1, else), (else, "One") -> "One"
(2, …Run Code Online (Sandbox Code Playgroud)