我用点语言制作了一棵树,类似于这里的树.
有什么方法可以让树向右扩展,而不是向下扩展(所以根节点在左边,子节点在右边).
我正在关注"真实世界Haskell",并且正在进行第2章,其中练习是创建一个"lastButOne"函数,它返回列表中倒数第二个值.我原来的代码是:
lastButOne xs = if null (tail (tail xs))
then head xs
else lastButOne (tail xs)
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,除非我给它一个只有1或2个项目的列表.所以我修改了它:
lastButOne xs = if null xs || null (tail xs)
then head xs
else
if null (tail (tail xs))
then head xs
else lastButOne (tail xs)
Run Code Online (Sandbox Code Playgroud)
哪个检查它是否只有1个或2个项目,但如果由于调用头部而只有1个项目则会失败.我的问题是我不能想到除了"头xs"以外的任何其他东西,理想情况下我希望它返回null或类似的东西,但我找不到允许该函数仍然存在的"null"多态.
在C++中,有两种方法可以将函数传递给另一个看似等效的函数.
#include <iostream>
int add1(int i){ return i+1; }
int add2(int i){ return i+2; }
template <int (*T)(int) >
void doTemplate(int i){
std::cout << "Do Template: " << T(i) << "\n";
}
void doParam(int i, int (*f)(int)){
std::cout << "Do Param: " << f(i) << "\n";
}
int main(){
doTemplate<add1>(0);
doTemplate<add2>(0);
doParam(0, add1);
doParam(0, add2);
}
Run Code Online (Sandbox Code Playgroud)
doTemplate接受一个函数作为模板参数,而doParam接受它作为函数指针,它们似乎都给出了相同的结果.
使用每种方法之间的权衡是什么?
是否有任何库可以让您在 C++ 单元测试中比较文件?理想情况下,这适用于 Boost 单元测试。
我正在考虑以下方面的功能:
CHECK_FILES_EQUAL('output.txt', 'reference.txt');
Run Code Online (Sandbox Code Playgroud)
如果文件相等,则测试失败(可能显示失败的行)。
谢谢
我有两个类 - Task(实现Comparable)和DeadlinedTask(DeadlinedTask扩展Task).对于他们每个人,我编写了一个重载的compareTo函数(每个函数都有compareTo(Task)和compareTo(DeadlinedTask)).
我的想法是,我可以按类别排序常规任务,截止日期前截止DeadlinedTasks,但我也希望所有DeadlinedTasks都排在任务之上.
当我在一系列任务(没有DeadlinedTasks)的列表上调用Collections.sort(myListOfTasks)时,一切都像魅力一样.但是,当我有一个Tasks和DeadlinedTasks的列表时,对象会改变顺序,但它们没有完全排序.
我已经尝试在interclass比较中返回1以外的数字(1,1000,1000000都做了同样的事情).有没有办法通过compareTo和Collections.sort来做到这一点,我可以使用不同的java功能,还是我必须编写自己的搜索功能(作为比较器?)?
任务比较方法:
public int compareTo(Task other){
if(this.GetCategory().compareTo(other.GetCategory())==0)
return this.GetName().compareTo(other.GetName());
else
return this.GetCategory().compareTo(other.GetCategory());
}
public int compareTo(DeadlinedTask other){
return 1;
}
Run Code Online (Sandbox Code Playgroud)
DeadlinedTask compareTo方法:
public int compareTo(Task other){
return -1;
}
public int compareTo(DeadlinedTask other){
if(this.GetDeadline().compareTo(other.GetDeadline())==0)
return this.GetName().compareTo(other.GetName());
else
return this.GetDeadline().compareTo(other.GetDeadline());
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
我打算写一个简单的游戏来测试我对函数式编程的理解.执行主循环的功能方法是递归它,但是当生成越来越多的堆栈帧时,这不会占用内存吗?
谢谢
一个例子来自如何在没有可变状态的情况下做任何有用的事情?
// imperative version
pacman = new pacman(0, 0)
while true
if key = UP then pacman.y++
elif key = DOWN then pacman.y--
elif key = LEFT then pacman.x--
elif key = UP then pacman.x++
render(pacman)
// functional version
let rec loop pacman =
render(pacman)
let x, y = switch(key)
case LEFT: pacman.x - 1, pacman.y
case RIGHT: pacman.x + 1, pacman.y
case UP: pacman.x, pacman.y - 1
case DOWN: pacman.x, pacman.y + 1
loop(new pacman(x, y))
Run Code Online (Sandbox Code Playgroud) 我有一个 graphviz (2.38.0 (20140413.2041)) 图,其中每个节点都包含几行文本,我希望不同的行具有不同的样式。目前我有:
digraph G{
stylesheet = "styles.css";
graph[rankdir=BT];
node[shape=box];
Andrew[label=<
Andrew
<br />Red
<br />34
>];
James[label=<
James
<br />Yellow
<br />26
>];
Andrew -> James;
}
Run Code Online (Sandbox Code Playgroud)
使用样式表:
.name {
font-weight: bold;
}
.age{
color: blue;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够使用以下功能:
Andrew[label=<
<font class="name">Andrew</font>
<br />Red
<br /><font class="age">34</font>
>];
Run Code Online (Sandbox Code Playgroud)
但不幸的是 dot 给了我:
Warning: Illegal attribute class in <FONT> - ignored
Warning: Illegal attribute class in <FONT> - ignored
Run Code Online (Sandbox Code Playgroud)
我进行了搜索但找不到任何东西,所以我不确定是否有首选的方式来完成我想要实现的目标(例如某种生成重复格式的宏),我错过了.
我有一个个人项目,我怀疑它会超过20对header/cpp文件.我想知道让每个头文件和cpp文件包含它需要的其他文件(或使用前向声明)是否更好,或者让每个文件都包含"Includes.hpp",后者又包括所有标准库,给出每个类的前向声明,然后包括我的所有其他标题.
我可以看到,使用一个大头文件:
这是一个好主意吗?