2012-01-19 10 views
1

挿入リストとカウントに依存するブール値リストを作成するコードが必要です。たとえば、ユーザーがを指定した場合、リスト[0,1,2,3,4,5,6,7,8,9,10]および件数= 2の場合、コードはとなります。bool List [true、false 、]真、偽、真、偽、真、偽、真、偽、真ブール値のブール値リストを作成する方法は、挿入されたカウントとリストに依存します。#

場合は、数= 3それはブール一覧を行います[真、偽、偽、真、偽、偽、そして数= 4[真、偽、偽、偽、真、偽、偽、偽、真の場合

]、真偽、偽、真、偽false]など....

私は次のコードを書いていますが、このコードは間違っていると思います。私はf#に新しいので、助けが必要です。ありがとう。

let Group (s1 : List) (c : int) = 
     let lenght = List.length(s1) 
     (lenght) 
      let rec MakeBool (count : int) (boolist : List) = 
       while lenght > 0 do 
        if lenght % count = 0 then boolist = true::boolist 
        if lenght % count <> 0 then boolist = false::boolist  
        lenght = lenght - 1 
        MakeBool count boolist 
+0

それは言ってあなたの質問を拡大する価値があるかもしれません*なぜあなたはこれをしたいのですか?あなたはいくつかの有効な答えを持っていますが、ちょっとした文脈を与えることによって、この(むしろ奇妙な)構造を避けるための提案を得ることができます。 – Kit

答えて

3

上位(推奨)機能の使用:独自の関数ローリング

let group ls c = 
    ls |> List.mapi (fun i _ -> i%c = 0) 

を:

let group ls c = 
let length = List.length ls  
let rec makeBool count acc = 
    if count = length then acc // Come to the end of ls, return the accummulator 
    elif count%c=0 then // Satisfy the condition, prepend true to the accummulator 
    makeBool (count+1) (true::acc) 
    else // Otherwise prepend false to the accummulator 
    makeBool (count+1) (false::acc) 
List.rev (makeBool 0 []) // The accummulator is in backward order, reverse it 
+0

注:あなたのソリューションでは、値ではなく、リストのインデックスを調整したいと考えています。彼のサンプルリストを考えれば、彼が望むものにすることができたかもしれないが、私はそれが重要であると思った。 – Benjol

1

このようにしますか?

let Group l c = [ for l' in 0..l -> (l' % c) = 0 ] 

signatueがGroup : int -> int -> bool list

  • [a..b] a..bにおけるxの[
  • (包括的の両方)AからBへの整数のリストを作成している - > F (x)]は同じですが、各要素にfを適用します。
  • (a%c)= 0は、aが法cであるかどうかをチェックするだけです。

// H

+0

どちらの解決策も素晴らしいです、あなたのフィードバックをお寄せいただきありがとうございます:-) –

関連する問題