Skip to content

Instantly share code, notes, and snippets.

@ncaq
Last active June 15, 2022 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncaq/fb4ba6ede5dbde3170822fd21c249930 to your computer and use it in GitHub Desktop.
Save ncaq/fb4ba6ede5dbde3170822fd21c249930 to your computer and use it in GitHub Desktop.
[Rubyist のための他言語探訪 【第 13 回】 Prolog](https://magazine.rubyist.net/articles/0021/0021-Legwork.html) のHaskell実装
import Control.Applicative
import Control.Monad
import qualified Data.List as L
main = print (兄弟 sally erica :: [Bool])
兄弟 x y = do
xa <- 子の親 x
ya <- 子の親 y
guard $ xa == ya
pure True
子の親 x = 子の母 x <|> 子の父 x
子の母 x
| x == sally = pure judy
| otherwise = empty
子の父 x
| x `L.elem` [sally, erica] = pure tom
| x == tom = pure mike
| otherwise = empty
erica = "erica"
judy = "judy"
mike = "mike"
sally = "sally"
tom = "tom"
@ncaq
Copy link
Author

ncaq commented May 6, 2022

自由変数の概念が無いのでそれぞれ固定化された方向にしかデータベースを構築出来ないのが残念。
全部取り出して実行するとかは大きくなると効率悪すぎるので出来ないですし。
この範囲だけ見るとTypeScriptとかで実装した時とさほど変わらなく見えるかもしれない。
やはりこういう特化したコードの場合Prologの優位性は残ったままか。

@ncaq
Copy link
Author

ncaq commented May 6, 2022

この範囲だけ見るとRDBMSだけで良いのではという気もしますね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment