关于haskell:Expression-Problem-和-Calcit-相关引用笔记

  • Wiki https://en.wikipedia.org/wiki…
  • 知乎援用 https://www.zhihu.com/questio…
  • 中文简介 http://mgampkay.github.io/pos…
  • The Expression Problem and its solutions
  • More thoughts on the Expression Problem in Haskell
  • 3 ways to solve the expression problem
  • The Expression Problem in Rust
  • Solving the Expression Problem with Clojure(Protocol)

Calcit 示例代码

ns app.main

defrecord %expr-methods :eval

def %const $ %{} %expr-methods
  :eval $ fn (tp)
    nth tp 1

def %binary-plus $ %{} %expr-methods
  :eval $ fn (tp)
    &let
      pair $ nth tp 1
      + (nth pair 0) (nth pair 1)

def %const-2 $ .extend-as %const '%const-2
  , :stringify $ fn (tp)
    str "|(Const " (nth tp 1) "| )"

def %binary-plus-2 $ .extend-as %binary-plus '%binary-plus-2'
  , :stringify $ fn (tp)
    &let
      pair $ nth tp 1
      str "|(BinaryPlus " (first pair) "| " (last pair) "| )"

defn main ()
  echo $ .eval $ :: %const 1
  echo $ .eval $ :: %binary-plus $ [] 1 2

  echo $ .stringify $ :: %const-2 1
  echo $ .eval $ :: %const-2 1
  echo $ .stringify $ :: %binary-plus-2 $ [] 1 2
  echo $ .eval $ :: %binary-plus-2 $ [] 1 2

运行示例:

=>> bundle_calcit && cr -1
file created at ./compact.cirru
calcit_runner version: 0.4.16
1
3
(Const 1 )
1
(BinaryPlus 1 2 )
3
took 7.997ms: nil

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理