0
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?

More than 5 years have passed since last update.

リストの評価の遅延(force)

Posted at

http://www.amazon.co.jp/%E9%96%A2%E6%95%B0%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E5%85%A5%E9%96%80-%E2%80%95Haskell%E3%81%A7%E5%AD%A6%E3%81%B6%E5%8E%9F%E7%90%86%E3%81%A8%E6%8A%80%E6%B3%95%E2%80%95-Richard-Bird/dp/427406896X でパーサのとこでforceって出てきてなんのこっちゃいなと思ったんだけど、こういうことか。

force :: [(a, b)] -> [(a, b)]
force xs = (fst (head xs), snd (head xs)) : tail xs
> let x = force undefined
> let y = undefined
> null x
False
> null y
*** Exception: Prelude.undefined

undefinedを、未計算の値で次は1:undefined、さらに1:2:undefined・・・みたいに順次計算されていくリストと見るとよい。そういう観点で見ると、この手法によって特に無限リストにおいて、長さが1以上のリストであることを強制できる。

0
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
0
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?