1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

F#でHaskellのgroupぽいものを作る

Posted at

はじめに

HaskellのData.Listライブラリには関数"group"があります。この関数は、リスト内の隣接してる値が同じ場合、隣接した同じ値でリストにまとめてくれます。
この関数をF#でも使いたいなと思い作成したものをメモとして載せときます。

コード


let rec group (list: seq<'a>)  =
    match Seq.toList list with
        | [] -> []
        | x::xs ->
            let head = x :: List.takeWhile ((=) x) xs
            let tail = List.skipWhile ((=) x) xs
            head :: group tail

実行結果

group "abbccca" |> printfn "%A"
//実行結果: [['a']; ['b'; 'b']; ['c'; 'c'; 'c']; ['a']]
group [1;2;2;3;3;3;1] |> printfn "%A"
//実行結果: [[1]; [2; 2]; [3; 3; 3]; [1]]
group [|1;2;2;3;3;3;1|] |> printfn "%A"
//実行結果: [[1]; [2; 2]; [3; 3; 3]; [1]]
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?