Comments (7)
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.
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.
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.
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.
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.
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.
(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)
- Missing explanations on how to run Child AI HOT 1
- in child_ai.py hard to understand the curiosity_behavior function
- Canonical way to process and/or present complex expressions with nested subexpressions HOT 4
- Question regarding types and their understanding HOT 3
- Question regarding performance hacks HOT 6
- Recursion with multiple matches HOT 3
- Could (if) return True instead of its content? HOT 5
- How to emulate lambda calculus? HOT 17
- What is the canonical way to write queries to knowledge graph HOT 44
- Slow list creation HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from metta-examples.