2017-05-13 2 views
0

以下のF#コードで、firstElemが値を取得する方法は?私はこのlinkの下のコードを得ました。このF#コードで、firstElemはどのように値を取得していますか?

let rec quicksort list = 
    match list with 
     | [] ->       // If the list is empty 
      []       // return an empty list 
     | firstElem::otherElements ->  // If the list is not empty  
      let smallerElements =   // extract the smaller ones  
     otherElements    
     |> List.filter (fun e -> e < firstElem) 
     |> quicksort    // and sort them 
    let largerElements =   // extract the large ones 
     otherElements 
     |> List.filter (fun e -> e >= firstElem) 
     |> quicksort    // and sort them 
    // Combine the 3 parts into a new list and return it 
    List.concat [smallerElements; [firstElem]; largerElements] 

答えて

1

テキストのコピー時に、テキストの字下げがどうしようもなくなりました。リンク元のコードでは、smallerElementslargerElementsのletバインディングは、両方とも| firstElem::otherElements ->よりもインデントされています。つまり、firstElemはパラメータ/変数listの先頭との一致からその値を取得します。

編集:ヘッドという用語は、リストの頭と尾を指します。ヘッドは最初の要素で、テールは残りの要素のすべてです。例えば。

let ns = [1; 2; 3; 4] 
let h = ns.Head 
let t = ns.Tail 

は頭と尾が空のリストのために定義されていないことを警告して

val ns : int list = [1; 2; 3; 4] 
val h : int = 1 
val t : int list = [2; 3; 4] 

を返します。

match list with 
| firstElem::otherElements -> 

ラインはfirstElemotherElementsとテールとlistの頭と一致します。

+0

答えに感謝します!私は機能的でf#プログラミングに新しいです。あなたが話している "パラメータの頭"とは何ですか?あなたは私にそれのためのドキュメンテーションのリンクを与えることができますか? – user3587180

+0

私はここではっきりしていませんでした。時々あなたはリストが頭と尾であると言います。ヘッドは最初の要素で、テールは残りの要素のすべてです。私は答えに数行を追加します。 –

関連する問題