English 中文(简体)
F# 通过一名操作员,对某项职能提出论据
原标题:F# passing an operator with arguments to a function

如果“附加1”是指:

List.map ((+) 1) [1..5];;  //equals [2..6]
// instead of having to write: List.map (fun x-> x+1) [1..5]

所发生的情况是1起适用于(+)项,因为它是第一个论点,而清单项目作为第二个论点正在适用。 关于增加和重复,这一命令的论点就没有问题了。

我想从每个要素中删除第1部分(这很可能是一个共同的开端错误):

List.map ((-) 1) [1..5];;  //equals [0 .. -4], the opposite of what we wanted

1 适用于(-)作为第一种论点的(-)条,因此,不采用<条码>(清单_项目 - 1,我收到<条码>(1 - 清单_项目/代码)。 我可以把它改写为消极的,而不是贬低积极的一面:

List.map ((+) -1) [1..5];;
List.map (fun x -> x-1) [1..5];; // this works too

I m 寻求一种更清晰的写法,例如(-)__<>/code>,其中_指持者,如Arc 语。 这将使1成为-的第二种理由,从而列入清单。 因此,如果你想要在名单上标注<代码>divide,你可以写:

List.map ((/) _ 2) [2;4;6] //not real syntax, but would equal [1;2;3] 
List.map (fun x -> x/2) [2;4;6] //real syntax equivalent of the above

能否做到这一点,或者我是否必须使用<代码>(fun x ->x

最佳回答

您可以撰写一份短片,如:

let flip f x y = f y x

List.map (flip (-) 1) [2;4;6]

我可能会有yn子错,在F#中,我不是可怕的流利。

问题回答

F#中没有任何业务部分,即“Haskell”或“持股人”的论点(显然就是“Arc”)。 你可以按照另一个答复中的建议使用一个口头组合,以推翻论点次序,然后部分适用第一个(现为第二个)论点。

但是,我只想使用。

fun x -> x / 2

除非你重新玩 code手法,否则我不认为试图在座打上其他几个品,买你们的东西。





相关问题
F#: Storing and mapping a list of functions

I have a number of events that happen in a game. I want to control the time and order at which these events occur. For example: Event 1: Show some text on screen for N frames & play a sound ...

Creating Silverlight 3 applications with F#

Is there an easy way to create Silverlight 3 applications with F# (October CTP)? I have seen the F# for Silverlight, but that only works with the May CTP. I am using Visual Studio Integrated Shell ...

How To Change List of Chars To String?

In F# I want to transform a list of chars into a string. Consider the following code: let lChars = [ a ; b ; c ] If I simply do lChars.ToString, I get "[ a ; b ; c ]". I m trying to get "abc". I ...

Unzipping ZLIB compressed portions of a binary file

I m reading a file(a flash swf) from .Net 3.5 that has a header which states whether the body of the file/Stream is compressed or not. However, I m having problems-after I rewrap the basic File ...

Pretty print a tree

Let s say I have a binary tree data structure defined as follows type a tree = | Node of a tree * a * a tree | Nil I have an instance of a tree as follows: let x = Node (Node (...

F# String Pattern-Matching with Wildcards

As part of a project I have assigned myself as a way of improving my knowledge of F# and functional programming in general, I am attempting to write a string pattern-matching algorithm from scratch ...