English 中文(简体)
如何在F#中的Seq、清单或Array中找到最高指数
原标题:How to find max s index in a Seq, List or Array in F#
  • 时间:2010-01-10 12:44:12
  •  标签:
  • f#

<代码>Seq.max发现最大数目。 我愿有如下内容:Seq.findIndex

<代码>Seq.maxIndex> 回归最高要素指数。

最佳回答

我认为,你们正在寻找这样的东西:

let maxIndex seq = 
    fst (Seq.maxBy snd (Seq.mapi (fun i x -> i, xiii seqiiiiii

请注意,赋予这一职能一个空洞的顺序将导致出现“怀疑”。

(替代方式,用管道写成:

let maxIndex seq =  
    seq
    |> Seq.mapi (fun i x -> i, xiii
    |> Seq.maxBy snd 
    |> fst

iii

问题回答

为什么不单纯使用

let l=[1;2;5;3];;
Seq.findIndex  (fun x -> x= Seq.max l) l ;;

?

或者,Johan Kullbom在评论中建议:

"let m = Seq.max l in Seq.findIndex (fun x -> x = m) l"

如果是哪怕是哪一个更好的O(n)

However, the need to get the index looks to me like a imperative "code smell" .

In FP it s usually better to use existing functions before you roll your own. I now this in the eyes of a C programmer seems like a for(i (for(j construct but I bet that you probably really don t need to know the index if you start think in FP.

https://stackoverflow.com/questions/1496980/Finding-index-of-element-in-a-list-in-haskell> Haskell 名单要素调查指数:

PS. I can t resist. In Haskell (ghc) the way should probably be something like

let cmpSnd (_, y1) (_, y2) = compare y1  y2

let maxIndex l= fst $ maximumBy cmpSnd $ zip [0..] l

然而,由于F#中的用zi似乎允许在清单的长短不一的情况下用zi。 地图的使用或许是走路(F#中的米壳体版本)

let cmpSnd xs=  snd xs ;;

let zipIndex a= Seq.mapi (fun i x -> i,x) a;;

let maxIndex seq=fst (Seq.maxBy cmpSnd (zipIndex seq));;

而原因只是为了我能够提出一个名单。

let l= [[0;199;1];[4;4];[0;0;399]]

test with makeIndex l;; and decide that what I really want is a

let cmpSnd  (a,(xs: int list))  = Seq.sum  xs;;
let maxIndex  seq=fst (Seq.maxBy cmpSnd  (zipIndex seq));;

现在是失败和失败的时候了 指数

let maxIndexF seq maxF=fst (Seq.maxBy maxF (zipIndex seq));;

val l : int list list = [[1; 2; 199]; [3; 3]; [4; 1]; [0; 299]]

> maxIndexF l cmpSnd 
;;
val it : int = 3
> maxIndexF l cmpSnd
;;
val it : int = 2

放弃

let maxIndexF   maxF=fst << Seq.maxBy maxF << zipIndex ;;

maxIndexF  cmpSnd  l;;
maxIndexF  cmpSnd l;;




相关问题
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 ...

热门标签