Help us understand the problem. What is going on with this article?

Haskell で Kleene閉包:別解

More than 5 years have passed since last update.

「Haskell で Kleene閉包」 という記事を読んだのですが、「単純な再帰を使って初期値の与え方を変えれば、重複なしで「Kleene閉包」を求められるんじゃない」か、と考えて自分なりにコーディングしてみました。


とりあえず、Wikipedia の例はちゃんと求められるようです。

kleeneStar :: [[a]] -> [[a]]
kleeneStar xs = [] : loop xs
  where loop xs' = xs' ++ loop [y ++ z | y <- xs, z <- xs']
> take 15 $ kleeneStar ["ab", "c"]

> take 10 $ kleeneStar [['a'], ['b'], ['c']]


"iterate" を使用した定義を思いつきました。

kleeneStar :: [[a]] -> [[a]]
kleeneStar xs = concat $ iterate (\ys -> [y ++ x | y <- ys, x <- xs]) [[]]
好きなもの…Haskell, Scheme, Ruby
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away