我试图将此C#代码转换为F#:
double[,] matrix;
public Matrix(int rows, int cols)
{
this.matrix = new double[rows, cols];
}
public double this[int row, int col]
{
get
{
return this.matrix[row, col];
}
set
{
this.matrix[row, col] = value;
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我最大的问题是在F#中创建索引器.网络上的任何地方我找不到任何可以应用的东西.我包括了该类的其他几个部分,以防将索引器合并到Matrix类型中并不明显.所以一个好的答案将包括如何从这里的三个部分中制作一个完整的类型,以及可能需要的任何其他东西.另外,我知道F#powerpack中的矩阵类型,但是我试图通过将我理解的C#项目转换为F#来学习F#.
提前致谢,
短发
我正在读purescript维基,发现下面的章节这也解释了do在以下方面>>=.
什么>>=意思?
记谱法
do关键字为monadic表达式引入了简单的语法糖.
这是一个例子,使用monad作为
Maybe类型:Run Code Online (Sandbox Code Playgroud)maybeSum :: Maybe Number -> Maybe Number -> Maybe Number maybeSum a b = do n <- a m <- b let result = n + m return result
maybeSum取两个值类型Maybe Number,如果两个数字都不是,则返回它们的总和Nothing.使用do表示法时,返回类型必须有Monad类型的相应实例.声明可以采用以下形式:
a <- x不好意思x >>= \a -> ...xx >>= \_ -> ...如果这是最后一个陈述,那就是去掉或者只是x.- 一个让绑定
let a = x.注意缺少in关键字.例子
maybeSum desugars to …
我正在阅读伊德里斯的书,我正在进行第一次证明练习.
通过练习证明same_lists,我能够像这样实现它,作为匹配Refl力量x和y统一:
total same_lists : {xs : List a} -> {ys : List a} ->
x = y -> xs = ys -> x :: xs = y :: ys
same_lists Refl Refl = Refl
Run Code Online (Sandbox Code Playgroud)
但是,当我试图以同样的方式证明其他东西时,我会遇到不匹配的问题.例如:
total allSame2 : (x, y : Nat) -> x = y -> S x = S y
allSame2 x y Refl = Refl
Run Code Online (Sandbox Code Playgroud)
编译器说:
Run Code Online (Sandbox Code Playgroud)Type mismatch between y = y (Type of Refl) and x = …
考虑F#中的以下代码
let rec ordered xs =
match xs with
| [] | [_] -> true
| x1 :: x2 :: xs' -> x1 <= x2 && ordered (x2 :: xs')
Run Code Online (Sandbox Code Playgroud)
然后
let rec insert x xs =
match xs with
| [] -> [x]
| y :: ys -> if x <= y then x :: y :: ys
else y :: insert x ys
Run Code Online (Sandbox Code Playgroud)
最后
let insertKeepsOrder (x : int) xs = ordered xs ==> ordered (insert x xs) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将以下C#转换为F#:
public class Matrix
{
double[,] matrix;
public int Cols
{
get
{
return this.matrix.GetUpperBound(1) + 1;
}
}
public int Rows
{
get
{
return this.matrix.GetUpperBound(0) + 1;
}
}
public Matrix(double[,] sourceMatrix)
{
this.matrix = new double[sourceMatrix.GetUpperBound(0) + 1, sourceMatrix.GetUpperBound(1) + 1];
for (int r = 0; r < this.Rows; r++)
{
for (int c = 0; c < this.Cols; c++)
{
this[r, c] = sourceMatrix[r, c];
}
}
}
public double this[int row, int col]
{
get …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中实现了一些请求限制.它基于使用MemoryCache和根据限制到期的项目.基本上我尝试检索一个缓存项目,如果它不在那里我继续发出请求,如果是他们我检查最后一个请求和锻炼下一个可用时间我可以提出请求.我的问题在于我必须等待提出请求.
//This is a shortened version of what im doing, but the includes the required code
public void ThrottleRequest(string webservice)
{
if (cache.TryGet(webservice))
{
var timeToWait = GetTimeToWaitFromSomewhere();
Wait(timeToWait);
}
}
public async void Wait(TimeSpan timeToWait)
{
await Task.Delay(timeToWait); //This doesnt work
Thread.Sleep(timeToWait); //This works
}
Run Code Online (Sandbox Code Playgroud)
问题在于等待方法.如果我使用thread.sleep数字匹配并且请求被正确限制(即每秒1个请求)但我永远不想在生产环境中使用它.那么在这里异步等待的正确方法是什么?我滥用API还是什么?
有谁知道VS2017是否已经支持代码合同,如此处所见的C++ 17代码合同?
当我尝试使用它们时
explicit IniHandler(std::string fileName) [[expects: fileName != nullptr]]
{
this->fileName = fileName;
}
Run Code Online (Sandbox Code Playgroud)
它似乎不起作用.
我正在使用命令行选项/std:c++latest但仍然收到警告"标识符已被删除".
任何帮助很高兴:)
f# ×3
assert ×1
asynchronous ×1
c# ×1
c++ ×1
c++17 ×1
contract ×1
fscheck ×1
idris ×1
indexer ×1
matrix ×1
purescript ×1
throttling ×1
types ×1