小编Mys*_*uff的帖子

有没有办法断言某个函数被编译器识别为尾递归?

假设我已经在Haskell中编写了一个函数,并且想断言它是尾递归的,并且编译器会对其进行优化。有办法吗?

我知道有一种方法可以在Scala中使用@tailrec注释。

例:

import scala.annotation.tailrec

class Factorial2 {
  def factorial(n: Int): Int = {
    @tailrec def factorialAcc(acc: Int, n: Int): Int = {
      if (n <= 1) acc
      else factorialAcc(n * acc, n - 1)
    }
    factorialAcc(1, n)
  }
}
Run Code Online (Sandbox Code Playgroud)

haskell tail-recursion static-assert

11
推荐指数
1
解决办法
181
查看次数

Makefile-清洁时如何隐藏“无此文件或目录”错误?

我正在创建一个C项目作为作业。除源文件外,它还必须包含一个Makefile,该文件必须使用命令“ make”来编译可执行文件“ solution”,以及另一个使用命令“ make debug”来编译带有附加“ -g”参数的可执行文件“ solution.gdb”。为此,我决定制作一组单独的对象文件(“ * .do”文件)。

但是,“ make clean”命令必须从目录中删除所有对象和可执行文件。仅在使用一个命令(“ make”或“ make debug”)之后,当我尝试使用“ make clean”命令时,就会出现问题,因为它试图删除不存在的文件。

错误消息示例:

rm solution.o tree.o list.o commands.o solution.do tree.do list.do commands.do solution solution.gdb
rm: cannot remove 'solution.o': No such file or directory
rm: cannot remove 'tree.o': No such file or directory
rm: cannot remove 'list.o': No such file or directory
rm: cannot remove 'commands.o': No such file or directory
rm: cannot remove 'solution': No such file or directory
Makefile:30: recipe for target 'clean' failed …
Run Code Online (Sandbox Code Playgroud)

c makefile

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

Bash - 如何检查命令是否没有输出而不打印它?

我正在编写一个测试脚本。它使用作为输入传递的 *.in 文件执行程序,并使用diff命令将其输出与 *.out 文件进行比较。

但是,我不想打印diff输出,而是检查是否有,如果有,则将 *.in 文件名添加到失败测试列表中。

问题是我不知道如何检查命令是否产生输出而不打印它。

我现在的脚本:

failed_tests=""

for filename in $directory/*.in; do

    command=< ${filename} ./${program} | diff - ${filename%.in}.out

    # Check if command produces output.
    if command; then
        # Add filename to failed tests list.
        failed_tests="${failed_tests} ${filename}"
    fi

done

echo $failed_tests
Run Code Online (Sandbox Code Playgroud)

预先感谢您的所有答案。

bash

0
推荐指数
1
解决办法
9273
查看次数

标签 统计

bash ×1

c ×1

haskell ×1

makefile ×1

static-assert ×1

tail-recursion ×1