首页 > 解决方案 > 如何创建像树一样的数据结构

问题描述

对于我的项目,我需要构建一个树形结构。我正在寻找一种在叶子上种植它的方法。我通过使用 listy 结构简化了我失败的尝试:

my $root = a => (b => (c=> Nil));
my $here := $root;
while $here.value ~~ Pair {
  $here := $here.value;
}
$here = d => Nil;

这不起作用,因为我当然不能更改 Nil。无法分配给不可变的值我怎样才能使它工作?

谢谢, Theo van den Heuvel

标签: data-structuresraku

解决方案


我认为您收到“无法分配给不可变值”的错误消息是因为该值不是容器。这是我将叶节点设为容器的示例:

my $root = a => (b => (my $ = (c => Nil)));
my $here := $root;
while $here.value ~~ Pair {
  $here := $here.value;
}
$here = d => Nil;

现在,没有错误消息。


推荐阅读