【激突】関数型言語 VS オブジェクト指向言語2at TECH【激突】関数型言語 VS オブジェクト指向言語2 - 暇つぶし2ch■コピペモード□スレを通常表示□オプションモード□このスレッドのURL■項目テキスト520:デフォルトの名無しさん 12/04/22 21:05:31.50 // F#で入出力以外の副作用なし let rec input hands = printfn "合計が15になるように捨てるカードをスペース区切りで指定(ギブアップの場合は指定なし)" match System.Console.ReadLine() with | "" -> None | inp -> try let discardCount = inp.Split([|' '|]) |> Seq.map int |> Seq.countBy id |> Map.ofSeq let handCount = hands |> Seq.countBy id |> Map.ofSeq if discardCount |> Map.exists (fun n c -> defaultArg (Map.tryFind n handCount) 0 < c) || discardCount |> Map.toSeq |> Seq.sumBy ((<||) (*)) |> (<>) 15 then input hands else handCount |> Map.map (fun n c -> c - defaultArg (Map.tryFind n discardCount) 0) |> Map.toList |> List.collect (fun (n, c) -> List.replicate c n) |> Some with _ -> input hands let rec play hands stocks = if (hands, stocks) = ([], []) then printfn "おめでとう!" else let cards = stocks |> Seq.truncate (7 - List.length hands) |> Seq.toList let nowHands, nextStocks = hands @ cards, (stocks |> Seq.skip cards.Length |> Seq.toList) nowHands |> Seq.sort |> Seq.map string |> String.concat " " |> printf "手札 : %s, " printfn "山札の残り枚数 : %d" nextStocks.Length match input nowHands with Some nextHands -> play nextHands nextStocks | None -> () let random = System.Random() [for i in 1 .. 9 do yield! List.replicate 4 i] |> List.sortBy (fun _ -> random.Next()) |> play [] 次ページ最新レス表示レスジャンプ類似スレ一覧スレッドの検索話題のニュースおまかせリストオプションしおりを挟むスレッドに書込スレッドの一覧暇つぶし2ch