我只是试过这段代码:
$m=2
function f
{
$m=3
}
f
$m
Run Code Online (Sandbox Code Playgroud)
我希望该函数f将更$m改为3。但是实际输出仍然是2。为什么?$m是全局变量,在内部不有效f吗?
我有这个脚本:
1..10|%{
$_
if(($_ % 3) -eq 0)
{
"wow"
break
}
}
"hello"
Run Code Online (Sandbox Code Playgroud)
我期望从1到10,在遇到3时会显示“哇”,最后显示“你好”,但实际输出是:
1
2
3
kkk
Run Code Online (Sandbox Code Playgroud)
因此,“ break”似乎没有破坏管道中的%{}循环,而是破坏了整个程序?如何在继续执行后续语句的同时中断循环?
谢谢。
我有一个代码片段如下:
public static class M
{
public static int Plus(this int i, int p=6)
{
return i + p;
}
}
public static class N
{
public static int Plus(this int i)
{
return i + 10;
}
}
class Program
{
static void Main()
{
int i = 3.Plus();
Console.WriteLine(i);
}
}
Run Code Online (Sandbox Code Playgroud)
运行程序,输出"13",表示调用N类的扩展方法.为什么M类的方法不匹配?
然后,如果我删除N类,OK,调用类M的扩展方法,它会按预期输出"9".
所以我的问题是,是否有一个规则在C#或.net框架中确定,如果有多个匹配,将调用哪个扩展方法?这是否与重载解析规则或其他相关?
非常感谢.
我想知道 F# 是否有一个名为“flat”的函数来将地图奉为一个列表,比如 from
{(1,"hello"),(2, "world")}
Run Code Online (Sandbox Code Playgroud)
到
{1, "hello", 2, "world"}.
Run Code Online (Sandbox Code Playgroud)
Scala有这个功能,F#有吗?
换句话说,F# 列表是否可以包含不同类型的元素?谢谢。
我有一个数组定义:
> $a={"abc","xyz","hello"}
Run Code Online (Sandbox Code Playgroud)
然后,使用foreach来修改它,但似乎原始元素没有改变:
> foreach($i in $a){$i="kkk"+$i}
> $a
> "abc","xyz","hello"
Run Code Online (Sandbox Code Playgroud)
为什么foreach循环不修改元素?然后我尝试使用ForEach-Object,这一次,它甚至没有运行:
> $a|%{$_=$_+"kkk"}
Method invocation failed because [System.Management.Automation.ScriptBlock] does not contain a method named 'op_Addition'.
At line:1 char:6
+ $a|%{$_=$_+"kkk"}
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Run Code Online (Sandbox Code Playgroud)
这有任何语法错误吗?或者我的理解不正确?谢谢.
我有以下代码:
function f()
{
begin{$count=0}
process{$count+=10}
end{$count}
}
1..10|f # OK
1..10|%{
begin{$count=0}
process{$count+=10}
end{$count}
} # Error
Run Code Online (Sandbox Code Playgroud)
第一个"f"调用成功,而%{}块显示错误:
100
% : The script block cannot be invoked because it contains more than one clause. The Invoke() method can only be used on script blocks that contain a single clause.
At D:\Untitled1.ps1:13 char:7
+ 1..10|%{
+ ~~
+ CategoryInfo : InvalidOperation: (:) [ForEach-Object], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.ForEachObjectCommand
Run Code Online (Sandbox Code Playgroud)
但为什么?ForEach-Object不支持begin/ process/ end块?
[1..4]可能有助于产生
[1;2;3;4]
Run Code Online (Sandbox Code Playgroud)
但我希望生成这样的范围:
[10;8;6;4;2]
Run Code Online (Sandbox Code Playgroud)
如何使用范围语义来实现这个(间隔+降序)?在这种情况下,"for"循环是强制性的吗?
非常感谢.
在F#我可以这样定义:
let rec sum = function
| [] -> 0
| x::xs -> x + sum xs
Run Code Online (Sandbox Code Playgroud)
看起来很方便.在Haskell有通信吗?
似乎函数应该具有递归的名称(为了调用自身),那么lambda函数如何递归?
我搜索了维基百科,它说这可以通过"Y-combinator"来完成.我没有太多的数学理论背景,只是告诉我"Y-combinator"是由Haskell自己发现的.在Haskell语言中称为"fix"关键字,我试过:
Prelude> let fact = fix (\f n -> if n == 0 then 1 else n * (f (n-1)))
<interactive>:17:12: Not in scope: ‘fix’
Run Code Online (Sandbox Code Playgroud)
似乎失败,但如何使用"修复"关键字来做我所期望的?
我尝试使用"内联"来定义一个适合不同输入参数类型的函数:
> let x=2.0
- let inline f x=x+1
- f x;;
f x;;
--^
stdin(6,3): error FS0001: This expression was expected to have type
int
but here has type
float
Run Code Online (Sandbox Code Playgroud)
我希望在f上应用"inline"之后,我得到了一个通用函数调用"f".但似乎失败了.怎么纠正呢?
powershell ×4
f# ×3
foreach ×2
haskell ×2
recursion ×2
break ×1
c# ×1
definition ×1
dictionary ×1
flat ×1
function ×1
generics ×1
inline ×1
intervals ×1
iterator ×1
lambda ×1
list ×1
match ×1
overloading ×1
pipeline ×1
range ×1
scriptblock ×1
types ×1