我正在尝试在Perl6中编写一些逻辑语句.
我做了逻辑运算符:
multi sub prefix:<¬> ($n) {
return not $n;
}
multi sub infix:<?> ($n, $b) {
return ($n and $b);
}
multi sub infix:<?> ($n, $b) {
return ($n or $b);
}
multi sub infix:<?> ($n, $b) {
if $n == True and $b == True {
return True;
} elsif $n == True and $b == False {
return False;
} elsif $n == False {
return True;
}
}
multi sub infix:<?> ($n, $b) {
return $b …Run Code Online (Sandbox Code Playgroud) 有没有办法对形式参数进行赋值?
就像是:
sub myfunc($n) {
$n = $n + 5;
return $n;
}
Run Code Online (Sandbox Code Playgroud)
或者,我是否必须创建一个新变量并为其分配$ n的值?
因此,我希望能够编写一个功能,通过使用不同值的硬币,找出可以针对特定金额进行更改的所有方法.
所以,我写了一个函数coin,告诉你一个给定的数量,你可以为这个值做出多少改变,给定一个特定的值硬币,以及一个计算你可以改变多少种方式的函数,具有相同的类型下一个较小硬币的参数.
然后我尝试编写一个函数ladder,我想返回一个函数,对于硬币值的@array将返回一个函数需要一个形式参数$amt,该参数计算在给定值的情况下可以对该量进行更改的方式的数量数组中指定的硬币.
我尝试使用&coin带有.assuming方法的函数来添加硬币的值并构建适当的梯形图.不幸的是,当我尝试运行生成的函数时,它会挂起.
my @values = 2, 5, 10, 20, 50, 100, 200;
#the coin of value 1 is assumed as a base case
my &test = ladder(@values);
say &test(5);
sub ladder(@values) {
my &base = sub () { return @values.shift };
for @values {
&base = &coin.assuming(*,$_,&base);
}
return &base;
}
sub coin($amt,$value,&lesser) {
if $amt >= $value {
return &coin($amt-$value,$value,&lesser) + &lesser($amt);
} else {
return &lesser($amt); …Run Code Online (Sandbox Code Playgroud) 所以,我正在读一本书,"形式语言理论导论",它描述了一种语言L(G) = {a^n ++ b^n | n > 0}.
它有以下产品:
S -> ab | aSb
Run Code Online (Sandbox Code Playgroud)
因此会产生以下语言:
a, ab, aabb, aaabbb, ...
Run Code Online (Sandbox Code Playgroud)
我想知道如何使用Haskell的列表理解来创建这种语言.我知道我可以使用字符串进行列表理解,但我几乎是初学者,并且不确定如何获得像我想要的这些字符串的无限列表.
我想象的是:
[ x ++ y | x <- ["a","aa",..] y <- ["b","bb",..]]
Run Code Online (Sandbox Code Playgroud) 我正在努力使¬成为一个逻辑否定运算符.
¬ True;
multi sub prefix:<¬> ($n) {
return not $n;
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的程序时,它返回此错误:
Run Code Online (Sandbox Code Playgroud)$ perl6 test.pl6 ===SORRY!=== Error while compiling /home/devXYZ/test.pl6 Bogus statement at /home/devXYZ/test.pl6:1 ------> <BOL>?¬ True; expecting any of: prefix term
有谁知道原因可能是什么?
我试图弄清楚如何使用狡猾的方案从文件中读取一行。
当我要求它“读取端口”或“读取字符端口”时,它成功读取。
guile -c '(let ((port (open-input-file "foo.txt"))) (display (read port)) (newline) (close-port port))'
Run Code Online (Sandbox Code Playgroud)
但是,当我要求它读取行时,它失败了。
guile -c '(let ((port (open-input-file "foo.txt"))) (display (read-line port)) (newline) (close-port port))'
Run Code Online (Sandbox Code Playgroud)
有谁知道我做错了什么?我当前位于 foo.txt 所在的目录中。
只是想知道是否有人知道为什么Perl6的日志函数返回Num类型而不是Rat类型.
say (e*e).log.WHAT;
> (Num)
say (2/3).WHAT;
> (Rat)
Run Code Online (Sandbox Code Playgroud) 作为练习,我试图解析一些标准文本,它是shell命令的输出.
pool: thisPool
state: ONLINE
status: Some supported features are not enabled on the pool. The pool can
still be used, but some features are unavailable.
action: Enable all features using 'zpool upgrade'. Once this is done,
the pool may no longer be accessible by software that does not support
the features. See zpool-features(5) for details.
scan: none requested
config:
NAME STATE READ WRITE CKSUM
homePool ONLINE 0 0 0
mirror-0 ONLINE 0 0 0
ata-WDC_WD5000AZLX-00CL5A0_WD-WCC3F7NUE93C ONLINE 0 0 0 …Run Code Online (Sandbox Code Playgroud) perl6 ×6
constants ×1
currying ×1
declaration ×1
grammar ×1
guile ×1
haskell ×1
initializer ×1
new-operator ×1
parameters ×1
raku ×1
regex ×1
subroutine ×1