是否可以在F#中定义一个=>运算符,使其行为类似于C#的lambda语法?这将允许一个人写
x => y
Run Code Online (Sandbox Code Playgroud)
代替
fun x -> y
Run Code Online (Sandbox Code Playgroud)
我是一个F#初学者,我的直觉告诉我这是不可能的,因为fun看起来像一个特殊的形式.
我正在尝试在Linux上运行该教程.我安装gcc,cython,numpy,six.
我可以导入数据,但似乎有一些问题解压缩它.
有人可以帮忙吗?
Python 2.7.3 (default, Jun 22 2015, 19:43:34)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import g3doc.tutorials.mnist.input_data as input_data
>>> mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "g3doc/tutorials/mnist/input_data.py", line 175, in read_data_sets
train_images = extract_images(local_file)
File "g3doc/tutorials/mnist/input_data.py", line 60, in extract_images
buf = bytestream.read(rows * …Run Code Online (Sandbox Code Playgroud) 我想弄清楚如何让这个功能工作。我对 F# 非常无能,所以感谢解释,
let deriv (f:(float->float), dx: float) =
fun f:(float -> float) * dx:float -> x:float -> float
let (f, dx, x) = ((f(x + dx) - f(x))/dx)
Run Code Online (Sandbox Code Playgroud)
我用f:(float->float)错了吗?
我想创建自己的列表类型CountedList<'T>,其中包含一个普通的F#列表以及列表中#元素的计数(因此我不必遍历整个列表来获取元素数).
这是我的尝试:
type CountedList<'T> = {List: 'T list; Count: int}
static member Empty<'a> () = {List=List.empty<'a>; Count=0}
member this.AddOne(element) = {this with List=element::this.List; Count=this.Count + 1}
Run Code Online (Sandbox Code Playgroud)
我们的想法是从某种类型的CountList.Empty开始,然后通过AddOne方法添加一个元素.
但是,当我尝试使用以下方法创建空列表时:
let emptyDoubleList = CountedList.Empty<double>()
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:
The instantiation of the generic type 'CountedList' is missing and can't be inferred from the arguments or return type of this member. Consider providing a type instantiation when accessing this type, e.g. 'CountedList<_>'.
Run Code Online (Sandbox Code Playgroud)
然而直接这样做:
let directEmptyDoubleList = {List=List.empty<double>; Count=0}
Run Code Online (Sandbox Code Playgroud)
并没有给出警告.
为什么我会收到此警告?编译器是否应该能够意识到它是一个CountedList?此外,任何有关如何改进我的CountedList实现的建议也将受到赞赏.
这是完整的代码示例:
type …Run Code Online (Sandbox Code Playgroud) 尝试构建用F#编写的WPF应用程序时收到以下警告:
程序的主要模块为空:运行时不会发生任何事情
因此,我无法启动该应用程序.
我已经验证所有AssemblyInfo.fs文件的末尾都有一个"do()".我也玩过文件的顺序.
有什么建议?
UPDATE
我在项目结束时添加了一个文件.
该文件包含以下代码:
module Bootstrap
open System.Windows
[<EntryPoint>]
let main args =
System.Windows.Application.Current.Run() |> ignore;
// Return 0. This indicates success.
0
Run Code Online (Sandbox Code Playgroud)
当我尝试运行该文件时,我在Application.Current上遇到了一个空引用异常.
我的解决方案如下所示:
如何进行重构,使得得分高于特定数量的分数是无法代表的?
例如,如何使用以下代码并使编译器拒绝任何超过总分为11分的镜头?
let results = (player1, player2) |> makeFieldBasket TwoPointer
|> makeFoulShots ThreeFoulShots
|> makeFieldBasket TwoPointer
|> makeFoulShots TwoFoulShots
|> makeFieldBasket TwoPointer
Run Code Online (Sandbox Code Playgroud)
上面代码的输出如下:
val results : FoulShooter * FieldShooter =
(FoulShooter {Score = 11;}, FieldShooter {Score = 0;})
Run Code Online (Sandbox Code Playgroud)
现在我想构建我的代码,以便无法编译额外的镜头.
例如,我希望编译器拒绝超过11分的额外犯规:
let results = (player1, player2) |> makeFieldBasket TwoPointer
|> makeFoulShots ThreeFoulShots
|> makeFieldBasket TwoPointer
|> makeFoulShots TwoFoulShots
|> makeFieldBasket TwoPointer
|> makeFoulShots FoulShot
Run Code Online (Sandbox Code Playgroud)
目前,上述代码是合法的.
整个代码如下:
(*Types*)
type Player = { Score:int }
type FieldShot = TwoPointer| ThreePointer
type FoulShots = …Run Code Online (Sandbox Code Playgroud) 我有以下代码,测试失败:
open Xunit
open FsUnit.Xunit
let rec openOrSenior xs =
match xs with
| head :: tail when fst head >= 55 && snd head >= 7 -> "Senior" :: openOrSenior tail
| head :: tail -> "Open" :: openOrSenior tail
| [] -> []
[<Fact>]
let ``empty input gives empty result``() =
openOrSenior [] |> should equal List.empty
Run Code Online (Sandbox Code Playgroud)
测试失败,出现以下匹配错误
FsUnit.Xunit + MatchException:抛出了类型'FsUnit.Xunit + MatchException'的异常.预期:等于[]
实际:是[]
特定
// r is a System.Data.IDataRecord
var blob = new byte[(r.GetBytes(0, 0, null, 0, int.MaxValue))];
r.GetBytes(0, 0, blob, 0, blob.Length);
Run Code Online (Sandbox Code Playgroud)
和r.GetBytes(...)回报Int64
由于Array.zeroCreate和Array.init采取Int32如何创建一个空的数组,它是可能大于Int32.MaxValue?
以下行不编译:
| IsNeither -> sprintf "%i" // ???
Run Code Online (Sandbox Code Playgroud)
这是该行所属的功能:
let run = function
| IsFizzBuzz -> "Fizz Buzz"
| IsFizz -> "Fizz"
| IsBuzz -> "Buzz"
| IsNeither -> sprintf "%i" // Doesn't compile
Run Code Online (Sandbox Code Playgroud)
这是整个程序: 模块Temp
let (|IsFizz|IsBuzz|IsFizzBuzz|IsNeither|) = function
| n when n % 3 = 0 &&
n % 5 = 0 -> IsFizzBuzz
| n when n % 3 = 0 -> IsFizz
| n when n % 5 = 0 -> IsBuzz
| n -> IsNeither …Run Code Online (Sandbox Code Playgroud) 我需要prolog中的关系来将列表左移一个元素,这样就可以了
shift([a,b,c,d,e,f,g,h],3,Shifted).
Run Code Online (Sandbox Code Playgroud)
应该产生
Shifted = [d,e,f,g,h,a,b,c]
Run Code Online (Sandbox Code Playgroud)
你可以帮帮我吗?
这就是我所拥有的
shift([], []).
shift([H|T], L) :-
append(T, [H], L).
shift(0, L, L) :- !.
shift(N, L1, L2) :-
N1 is N-1,
shift(L1, L),
shift(N1, L, L2).
Run Code Online (Sandbox Code Playgroud)