小编mar*_*oou的帖子

模式匹配返回数学表达式的字符串表示

我必须编写一个带有表达式的函数转储

type expression = 
| Int of int
| Float of float
| Add of expression * expression
| Sub of expression * expression
| Mult of expression * expression
| Div of expression * expression
;;
Run Code Online (Sandbox Code Playgroud)

并返回它的字符串表示形式.例如:

dump (Add (Int 1, Int 2));;
dump (Mult (Int 5, Add(Int 2, Int 3)), Int 1)
Run Code Online (Sandbox Code Playgroud)

应该分别回来

- : string = "1+2"
- : string = "5*(2+3)-1"
Run Code Online (Sandbox Code Playgroud)

我写过这样的话:

let rec dump e = match e with
    | Int a -> string_of_int a …
Run Code Online (Sandbox Code Playgroud)

ocaml functional-programming

6
推荐指数
1
解决办法
216
查看次数

循环同步死锁

我在Java中有以下类

public class Counter {
    private int value;

    public Counter(int value) {
        this.value = value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public void decrement() {
        this.value--;
    }
    public int getValue() {
        return this.value;
    }
}

public class Cell extends Thread {

    private Object sync;
    private Counter counter;

    public Cell(Object sync, Counter counter) {
        this.sync = sync;
        this.counter = counter;
    }

    public void run() {
        for (int r=0; r<Simulation.ROUND_NUM; r++) {

            // do something

            synchronized(counter) {
                counter.decrement(); …
Run Code Online (Sandbox Code Playgroud)

java concurrency deadlock thread-safety

5
推荐指数
2
解决办法
2654
查看次数

使用Parse :: RecDescent

我有以下输入

@Book{press,
  author    = "Press, W. and Teutolsky, S. and Vetterling, W. and Flannery B.",
  title     = "Numerical {R}ecipes in {C}: The {A}rt of {S}cientific {C}omputing",
  year      = 2007,
  publisher = "Cambridge University Press"
}
Run Code Online (Sandbox Code Playgroud)

我必须为RecDescent解析器生成器编写语法.输出中的数据应该针对xml结构进行修改,并且应该如下所示:

<book>
    <keyword>press</keyword>
    <author>Press, W.+Teutolsky, S.+Vetterling, W.+Flannery B.</author>
    <title>Numerical {R}ecipes in {C}: The {A}rt of {S}cientific {C}omputing</title>

    <year>2007</year>
    <publisher>Cambridge University Press</publisher>
</book>
Run Code Online (Sandbox Code Playgroud)

应将附加和重复字段报告为错误(带有行号的正确消息,不再进行解析).我试着从这样的事情开始:

use Parse::RecDescent;

open(my $in,  "<",  "parsing.txt")  or die "Can't open parsing.txt: $!";

my $text;
while (<$in>) {
    $text .= $_;
}

print …
Run Code Online (Sandbox Code Playgroud)

perl grammar parser-generator

4
推荐指数
1
解决办法
1611
查看次数