Git Product home page Git Product logo

Comments (7)

Necr0x0Der avatar Necr0x0Der commented on August 14, 2024 1

BTW, my guess is that in @vsbogd implementation (let $tail (cdr-atom $conds) will also evaluate the whole tail. It may need a more careful consideration, but my first guess is that this

(= (my-case $x $conds)
  (if (== $conds ())
    (empty)
    (let ($cond $templ) (car-atom $conds)
      (if $cond
          $templ
          (let $tail (cdr-atom $conds) (my-case $x $tail)) ))))

will at least screen out evaluation of the tail if $cond is true. Unfortunately, $templ will be evaluated even if $cond is false.

from metta-examples.

vsbogd avatar vsbogd commented on August 14, 2024

Yes, case doesn't reduce conditions, but with current implementation you can calculate the result of the condition in advance and then compare it with expected result. For instance following will work:

(= (check $x $y)
  (case ($x (> $x $y)) (
    ((0 $_) 1)
    (($_ True) $x)
    ($_ $y)
  )))

from metta-examples.

DaddyWesker avatar DaddyWesker commented on August 14, 2024

Well, question was for general case and example was just to demonstrate my question. In general It could be needed to check several different conditions. Anyway, thanks for the answer.

from metta-examples.

vsbogd avatar vsbogd commented on August 14, 2024

In general It could be needed to check several different conditions.

I agree this approach is too verbose for many different conditions. You can also right your own case implementation:

(: my-case (-> %Undefined% Expression %Undefined%))
(= (my-case $x $conds)
  (if (== $conds ())
    (empty)
    (let ($cond $templ) (car-atom $conds)
      (let $tail (cdr-atom $conds)
        (if $cond $templ (my-case $x $tail)) ))))

(= (check $x $y)
  (my-case $x (
    ((== $x 0)  1)
    ((> $x $y) $x)
    (True $y)
  )))

But pay attention that check also should be modified. In implementation from description unification pattern and boolean conditions are mixed in a list of conditions. To simplify implementation one need to choose either using unification (then use library case) or conditioning (then use my-case).

from metta-examples.

Necr0x0Der avatar Necr0x0Der commented on August 14, 2024

Yes, case doesn't reduce conditions and works more like case in functional languages, otherwise something like this would work

(= (check $x $y)
    (case True
        (((== 0 $x) 1)
         ((> $x $y) $x)
         ($_ $y))))

but it doesn't. This difference should be explained in a tutorial.

from metta-examples.

Necr0x0Der avatar Necr0x0Der commented on August 14, 2024

Another funny was would be to write:

(= (check $x $y)
   (let $conds
        (((== 0 $x) 1)
         ((> $x $y) $x)
         ($_ $y))
       (case True $conds))
)

It seemingly works, but the difference with normal case is that all the branches will be evaluated. It's not a tragedy for simple cases, but ruins the idea sequential mutually-exclusive evaluation in case .

from metta-examples.

vsbogd avatar vsbogd commented on August 14, 2024

(let $tail (cdr-atom $conds) will also evaluate the whole tail.

Yes, unfortunately it is inevitable with current let semantics. It can be eliminated in minimal MeTTa using chain and eval wisely though.

from metta-examples.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.