English 中文(简体)
帮助我解释F#矩阵转换功能
原标题:Help me to explain the F# Matrix transpose function
  • 时间:2010-06-10 16:00:08
  •  标签:
  • f#

具备以下职能:

let rec transpose = function
    | (_::_)::_ as M -> List.map List.head M :: transpose (List.map List.tail M)
    | _ -> []

[[1; 2; 3]; [4; 5; 6]; [7; 8; 9]] |> transpose |> printfn "%A"

It works fine.
What does ( _ :: _ ) :: _ mean?
I don t understand the whole code!
Who can explain it?
Thank You!

I find the answer:
( _ :: _ ) :: _ is a pattern matching on value of type list of lists of ints


www.un.org/Depts/DGACM/index_spanish.htm i 书写:

let rec transpose (M:int list list) =
    match M with
    | hd::tl -> List.map List.head M :: transpose (List.map List.tail M)
    | _ -> []

It throw an runtime exception. Is there something wrong with hd?
Yes, it make something like [ [ ];[ ];[ ] ] when call List.tail, then it throws exception when call List.head!


Problem Solved!
Thank you all!

最佳回答

这项职能不是特别可读写的,这可能是你混淆的原因。 构件<代码>(_:_:_:_)是一种对“ > ts/em”清单数值的对应模式,其中规定,在你获得非豁免清单时,应先处理第一种情况。

也可以这样写。 这更令人信服,但应当清楚的是,这里正在发生的事情:

let rec transpose matrix = 
  match matrix with   // matrix is a list<list<int>>
  | row::rows ->      // case when the list of rows is non-empty
    match row with    // rows is a list<int>
    | col::cols ->    // case when the row is non-empty
      // Take first elements from all rows of the matrix
      let first = List.map List.head matrix
      // Take remaining elements from all rows of the matrix
      // and then transpose the resulting matrix
      let rest = transpose (List.map List.tail matrix) 
      first :: rest
    | _ -> []
  | _ -> [] 

如你所知,我们并不真正需要<代码>row,rows,col。 因此,原来的执行将标题改为_(该编码忽视了该清单的价值,只检查该清单可以按规定的方式加以分解)。

在休妻的情况下,我们拆除了这样的矩阵:

[ [ x; y; y ];                               [ y; y ] 
  [ x; y; y ];   =>  [ x; x; x] :: transpose [ y; y ]
  [ x; y; y ] ]                              [ y; y ] 

我希望,情况使你们更加清楚!

问题回答

<代码>(_:_:_)为配对模式。 <代码>_只是一个未使用的变量。 这相当于:

(a::b)::c as M -> List.map List.head M :: transpose (List.map List.tail M)

我很抱歉,要放弃过时的read子,但你的原始答案几乎正确。 唯一错误的是空洞名单的终止条件。 该法典应当研究这样的内容:

let rec transpose M = 
    match M with 
    | []::_ -> []
    | _ -> List.map List.head M::transpose(List.map List.tail M)

该地图head 上载在清单中,以提取第1栏,并用于构成第1行的预选,以取代其余各栏。





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

热门标签