DrRacket 中是否有与 set-car 相同的东西!并设置-cdr!在 SICP?
在许多网络文章中,函数式编程被表示为避免各种变量重新分配,因此仅推广"最终"变量,至少为了更好的阅读.
它们中的大多数都采用带有计数器变量递增的不良循环的样本.(就像着名的i++或者x = x + 1.这里有一篇鲍勃叔叔的文章说明:FP第1集
因此,这些文章表明,依靠可变变量经常导致副作用,特别是防止我们所谓的"参照透明度",因此,更难以构建在多线程或更好的多处理器上运行的程序.
我的问题是:众所周知,i++通常是一个线程LOCAL变量,因此即使并发处理也不会出现问题.
为什么选择一个像带有局部变量的循环这样的例子作为赋值的缺点,并且允许直接得出结论并发编程存在风险?这两件事与我严格无关.
为了更清楚,为什么不选择全局变量(或字段对象)的重新分配,这显然enemy是并发编程,而不像Java那样过度使用所有的锁定模板.
我真的认为这个循环示例并不是将函数式编程的好处传递给命令式程序员的最好例证.
此外,它导致与"noob"函数程序员混淆,因为Scala例如在List.scala类中使用了很多while循环模式:
override def take(n: Int): List[A] = {
val b = new ListBuffer[A]
var i = 0
var these = this
while (!these.isEmpty && i < n) {
i += 1 // reassignment here
b += these.head
these = these.tail
}
if (these.isEmpty) this
else b.toList
}
Run Code Online (Sandbox Code Playgroud) functional-programming scala variable-assignment concurrent-programming
关于 C 编程,我在示例代码中看到了几种情况,其中指针赋值涉及右侧操作数的显式转换,即使它已经与左侧操作数的类型匹配。举个例子:
void memcpy(u8int *dest, const u8int *src, u32int len)
{
const u8int *sp = (const u8int *)src;
u8int *dp = (u8int *)dest;
for(; len != 0; len--) *dp++ = *sp++;
}
Run Code Online (Sandbox Code Playgroud)
当然,该函数也在头文件中正确声明为:
void memcpy(u8int * dest, const u8int * src, u32int len);
Run Code Online (Sandbox Code Playgroud)
该函数已经将变量“src”定义为“const u8int *”类型,那么为什么在分配给也是“const u8int *”类型的“sp”时会给出显式转换?将“dest”分配给“dp”也是如此。
最初分配以下两类变量:
类实例的实例变量.
最初分配的结构变量的实例变量.
现在最初分配的结构变量意味着什么?
我们不是在谈论局部变量,对吗?那么我们在谈论在函数成员定义中使用的字段变量(在这两个类别中)?
澄清这一点将非常感激.并提前感谢!
以下简单的代码返回,在我看来,一个意外的结果:
srand(101)
# create a multidimensional array that will house 3 matrices of dimensions 2x2
A = Array(Array{Float64,2},3)
# initialise array with zero matrices (is this the culprit?)
fill!(A, zeros(2,2))
# populate array (some dummy computation to illustrate my problem)
for ii=1:2
for jj=1:2
aux = randn(1,3)
for dd=1:3
A[dd][ii,jj]=aux[dd]
end
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我的数组A读取:
3-element Array{Array{Float64,2},1}:
2x2 Array{Float64,2}:
1.2821 -2.10146
-1.00158 1.8163
2x2 Array{Float64,2}:
1.2821 -2.10146
-1.00158 1.8163
2x2 Array{Float64,2}:
1.2821 -2.10146
-1.00158 1.8163
Run Code Online (Sandbox Code Playgroud)
当我实际创建它们时,为什么三个2x2矩阵相同?
我确实理解在将数组分配给Julia中的数组时必须要小心,但不知何故错误使我无法理解.
我发现的有趣的事情是,如果我初始化A如下:
for dd=1:3
A[dd] …Run Code Online (Sandbox Code Playgroud) 我想将一个 bash 变量分配为两个词段的集合。bash 变量类似于字符串列表,其中每个字符串是用引号括起来的两个单独的单词)。然后我尝试在 for 循环中使用此变量,以便每次迭代将循环变量分配给两个词段之一。
这是我的代码:
#!/bin/bash
variable='"var one" "var two" "var three"'
for i in $variable
do
echo $i
done
Run Code Online (Sandbox Code Playgroud)
我想要的输出是:
var one
var two
var three
Run Code Online (Sandbox Code Playgroud)
但我现在得到的
"var
one"
"var
two"
"var
three"
Run Code Online (Sandbox Code Playgroud) I discovered today that you can use and in variable assignment, similarly to how or is used. I rarely come across using or in this way, but have never even heard of people using and this way. Is this too obscure of a feature to recommend using or are there some concrete use cases where it helps with code clarity or brevity?
a = 1
b = 3
# c is equal to b unless a or b is False; …Run Code Online (Sandbox Code Playgroud) 在 C++ 中,以下是有效的,我可以毫无问题地运行它
int main(){
if (int i=5)
std::cout << i << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,即使以下内容也应该有效,但它给了我一个错误
if ((int i=5) == 5)
std::cout << i << std::endl;
Run Code Online (Sandbox Code Playgroud)
错误:
test.cpp: In function ‘int main()’:
test.cpp:4:10: error: expected primary-expression before ‘int’
if ((int i=5) == 5)
^
test.cpp:4:10: error: expected ‘)’ before ‘int’
test.cpp:5:36: error: expected ‘)’ before ‘;’ token
std::cout << i << std::endl;
^
Run Code Online (Sandbox Code Playgroud)
此外,在 c++17 下面的代码也必须有效,但它再次给我一个类似的错误
if (int i=5; i == 5)
std::cout << i << std::endl; …Run Code Online (Sandbox Code Playgroud) 我是 C++ 新手,这是我在计算机科学领域的第一年……我只是想问……有没有办法让我在标题中的内容起作用?
为了进一步解释我的意思,这里有一个例子:
template <class dataType>
class squareMatrix{
private:
int size_side;
dataType *mainPtr;
public:
squareMatrix(int n){
this->size_side = n;
mainPtr = new dataType[n*n];
}
void index(int row, int column, dataType value){
mainPtr[row+(size_side*column)] = value;
}
};
Run Code Online (Sandbox Code Playgroud)
如您所见,我需要使用此方法来操作矩阵中的索引
squareMatrix<int> obj(2); // created a matrix of 2x2 size
obj.index(0,0,10); // here is the method to store the number 10 in the 0,0 index
Run Code Online (Sandbox Code Playgroud)
然后我的问题是,有没有办法把它变成这个?
obj.index(0,0) = 10;
Run Code Online (Sandbox Code Playgroud)
有没有办法使用“=”或赋值运算符来代替向方法添加一个额外的参数?
下面是我的 Ruby on Rails 代码 -
inactive_list = [1,2,3,4,5]
raw_data = []
data = {
name: "Test",
full_name: "Test data"
}
if inactive_list.include? <<id of data>>
data[:active] = false
else
data[:active] = true
end
raw_data << data
Run Code Online (Sandbox Code Playgroud)
对于 if...else 语句,我收到 Rubocop linting 错误。我尝试进行一些更改,但无法修复 linting Rubocop 错误。
C: Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.
Run Code Online (Sandbox Code Playgroud)