我在RosettaCode上遇到了这段代码
constant @primes = 2, 3, { first * %% none(@_), (@_[* - 1], * + 2 ... Inf) } ... Inf;
say @primes[^10];
Run Code Online (Sandbox Code Playgroud)
在显式生成器块内:
1- @_s指的是什么序列?
2-第一个*指的是什么?
3- *in @_[* - 1]和next *指的是什么?
4-序列如何(@_[* - 1], * + 2 ... Inf)用于查找素数?
谢谢。
如何将数组的每个元素重现x次?
例如for my @a=<blu red>;,x = 5,结果应该像
(blu blu blu blu blu red red red red red)
我想出了这个
say flat map { ($_, $_, $_, $_, $_) }, @a;
Run Code Online (Sandbox Code Playgroud)
但是当然对于x的任意值,这是不实际的。
实际上如何做?谢谢。
我在reddit遇到了一个编程问题(看一下问题的链接)
这是 Python 中的解决方案之一:
s="112213"
k=2
result=0
for i in range(len(s)):
num_seen = 0
window = {}
for ind in range(i, len(s)):
if not s[ind] in window:
num_seen += 1
window[s[ind]] = 1
else:
window[s[ind]] += 1
if window[s[ind]] == k:
num_seen -= 1
if num_seen == 0:
result +=1
elif window[s[ind]] > k:
break
print(result)
Run Code Online (Sandbox Code Playgroud)
我已经尝试将此解决方案移植到 Raku 中,这是我的代码:
my @s=<1 1 2 2 1 3>;
my $k=2;
my $res=0;
for ^@s {
my $seen = 0;
my …Run Code Online (Sandbox Code Playgroud) 我在rosettacode遇到过这段代码
my @pascal = [1], { [0, |$_ Z+ |$_, 0] } ... Inf;
.say for @pascal[^4];
# ==>
# [1]
# [1 1]
# [1 2 1]
# [1 3 3 1]
Run Code Online (Sandbox Code Playgroud)
在显式生成器块中,我知道诸如列表展平和|zip 运算符之类的各个运算符是如何Z+工作的,但我很难理解它们如何协作生成下一个数组。有人可以详细解释它是如何工作的吗?谢谢你。
注意:为简洁起见,代码略有重新排列,即它与 Rosetta 中的代码在表面上有所不同。
这是代码:
my @s=<a b c d>;
for @s.kv {
for ($^k ... @s.elems) {
printf("%s ", $^v);
}
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
预期输出为:
# a b c d
# b c d
# c d
# d
Run Code Online (Sandbox Code Playgroud)
但它给出了这个错误(可能等等)
key 0, val 1 Too few positionals passed; expected 2 arguments but got 1
Run Code Online (Sandbox Code Playgroud)
它看起来像主循环的位置的变量$^k,并$^v不能在内环内使用。如何解决?谢谢。更新:修复了内循环内的错字
序幕
在Raku有一个概念叫infinite listAKAlazy list中定义和使用的一样:
my @inf = (1,2,3 ... Inf);
for @inf { say $_;
exit if $_ == 7 }
# => OUTPUT
1
2
3
4
5
6
7
Run Code Online (Sandbox Code Playgroud)
我想在 Common Lisp 中实现这种事情,特别是一个无限的连续整数列表,例如:
(defun inf (n)
("the implementation"))
Run Code Online (Sandbox Code Playgroud)
以至于
(inf 5)
=> (5 6 7 8 9 10 .... infinity)
;; hypothetical output just for the demo purposes. It won't be used in reality
Run Code Online (Sandbox Code Playgroud)
然后我将使用它进行这样的惰性评估:
(defun try () ;; catch and dolist …Run Code Online (Sandbox Code Playgroud) 我正在尝试将用 Python 编写的递归问题(此处的第 4 个问题,请参阅其 repo 页面了解详细信息)转换为(通用)Lisp
这是我为了可读性稍微编辑过的 Python 代码:
def coin(target,coins,res):
# Default output to target
min_coins = target
# Base Case
if target in coins:
res[target] = 1
return 1
# Return a known result if it happens to be greater than 1
elif res[target] > 0:
return res[target]
else:
# for every coin value that is <= than target
for i in [c for c in coins if c <= target]:
num_coins = …Run Code Online (Sandbox Code Playgroud) 我编写了一个代码来使用显式生成器内的数组变量来计算斐波那契数列,如下所示:
my @fib = [0],[1],-> @a, @b {[|@a Z+ |@b]} ... Inf;
say @fib[^6];
Run Code Online (Sandbox Code Playgroud)
这按预期工作。但是当我在同一代码中使用标量变量时,它也可以工作:
my @fib_v2 = [0],[1],-> $a, $b {[|$a Z+ |$b]} ... Inf;
say @fib_v2[^6];
Run Code Online (Sandbox Code Playgroud)
它们可以称为指向数组的标量变量吗?当它们以这种方式使用时,它们被称为什么?
请注意,我浏览了在线 Raku 文档,但很难发现特定信息,即是否可以使用标量变量引用数组。
我正在尝试使用一个变量,并在一个步骤中为其分配一个表达式:
给定的(样本)代码
my @l=<a b c d e f g h i j k>;
my $i=0;
while $i < 7 {
say @l[$i];
$i= ($i+1) * 2;
}
# Output:
# a
# c
# g
Run Code Online (Sandbox Code Playgroud)
所需的功能:
my @l=<a b c d e f g h i j k>;
my $i=0;
say @l[$i =~ ($i+1) * 2] while $i < 7;
# Here, first the @l[$i] must be evaluated
# then $i must be assigned to the expression
# ($i+1) …Run Code Online (Sandbox Code Playgroud) 我正在尝试在这样的散列的键或值上使用any或none:
my %w=(a => 1, b => 2);
say %w.keys; # works
say so 'a' == %w.keys.any; # doesn't work
Run Code Online (Sandbox Code Playgroud)
我已经检查了 Raku 文档的哈希和地图部分,但无法解决这个问题。如何解决?谢谢。
我想连续打印偶数,但我不能。
use Terminal::ANSIColor;
# Wanna print even numbers in red
for <1 2 3 4>
{ $_ %2 == 0 ?? say color('red'),$_,color('reset') !! say $_ }
Run Code Online (Sandbox Code Playgroud)
printf 似乎不适用于Terminal::ANSIColor指令,put也不起作用。
是否有任何开关可以say使其在没有换行符的情况下打印?如何Terminal::ANSIColor连续打印那些 格式化的部分?
在我的 Windows XP 盒子上,sbcl-1.4.14我已经安装了ASDF使用
(load "C:\\Program Files\\clisp-2.49\\asdf\\asdf.lisp")
(require :asdf)
(push "C:\\Documents and Settings\\mayhem\\lisp\\iterate\\" asdf:*central-registry*)
Run Code Online (Sandbox Code Playgroud)
在 SLIME
(require :iterate)
(iterate (for i from 1 to 5) (collect (* i i)))
Run Code Online (Sandbox Code Playgroud)
给出变量 I 未绑定错误
如果我这样做(in-package :iterate),上面的代码可以正常工作,但是这一次exit我定义的熟悉的函数和其他函数.sbclrc停止工作,The function ITERATE::EXIT is undefined例如,它们会给出错误类型。
如果我这样做(use-package :iterate),那么它会[Condition of type NAME-CONFLICT]出错。
所以我开始像这样使用这个包:
(iterate:iterate (iterate:for i from 1 to 5) (iterate:collect (* i i)))
但我想你会同意这是一种糟糕的风格。
如何iterate正确使用?
注意:我看过有关非常相似问题的帖子,但没有帮助。关于这个特定问题的帖子或文章并不多。
我想乘用浮列表元素reduce会同multiplies:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <numeric>
using namespace std;
int main () {
vector<float>series{2, 1.91421, 2.06538, 2.25, 2.43607};
float result = reduce(series.begin(), series.end(), 1, multiplies<float>() );
cout << "result: " << result << endl; // it's 29
// it must have been 43.340291222788287
}
Run Code Online (Sandbox Code Playgroud)
如何正确地做到这一点?请注意,我想尽可能地将它reduce与 结合使用multiplies,而不是任何其他函数,例如transform_reduce或任何其他方法。