Comments (6)
Iterative variant should be quicker, but in current metta interpreter's state it is quite opposite due to interpreter cashes all calls during evaluation
To me the second variant is quicker. In both minimal MeTTa and Rust interpreter.
from metta-examples.
Apparently, nested if
s may add some overhead. However, the code in these examples should be pretty straightforwardly compilable, which we may have in the near future. At the same time, the code like this
(= (fac2 0) 1)
(= (fac2 $x)
(if (> $x 0)
(* (fac2 (- $x 1)) $x)
(empty)))
uses non-determinism for a deterministic function, which is definitely not an idiomatic way to implement it, and involves (empty)
, which requires additional explanations. Even if it is faster, it's not a good idea for a tutorial to show such code.
There are other ways to implement fact
of fib
, and to avoid nested if
s for mutually exclusive branches (instead of using non-determinism which is for non-exclusive branches), e.g. with the use of case
:
(= (fac3 $x)
(case $x
((0 1)
($_ (* (fac3 (- $x 1)) $x)))))
Whether to use case
or not may depend on how the tutorial is organized.
Maybe, transforming this code
(= (fib $n)
(if (== $n 0)
0
(if (== $n 1)
1
(+ (fib (- $n 1)) (fib (- $n 2))))))
to this
(= (fib3 $n)
(case $n
((0 0)
(1 1)
($_ (+ (fib3 (- $n 1)) (fib3 (- $n 2)))))))
is exactly the place where case
can be introduced. Although case
differs from nested if
in MeTTa in a deeper way than in other languages, so it should also be described carefully
from metta-examples.
Iterative variant should be quicker, but in current metta interpreter's state it is quite opposite due to interpreter cashes all calls during evaluation
To me the second variant is quicker. In both minimal MeTTa and Rust interpreter.
It is weird, but you're right. It was quite opposite on the previous metta versions for me. I haven't tested time difference between these two variants after updating metta.
from metta-examples.
@Necr0x0Der Thank you for explanations. One question regarding case
- what $_
symbol means? In case of this function:
(= (fib3 $n)
(case $n
((0 0)
(1 1)
($_ (+ (fib3 (- $n 1)) (fib3 (- $n 2)))))))
as I understand for (== $n 0) result will be 0, for (== $n 1) result will be 1, for $_ (everything else?) result will be sum of recursions. Is this symbol $_
applicable only in case of case
? Or it has some other use cases?
from metta-examples.
what $_ symbol means?
In MeTTa it is just a normal variable. Name _
is chosen to be familiar to users of other languages where _
means match anything. Keep in mind that if you want to use couple of such variables in same context you need to name it differently: $_
and $__
or $_1
.
from metta-examples.
what $_ symbol means?
In MeTTa it is just a normal variable. Name
_
is chosen to be familiar to users of other languages where_
means match anything. Keep in mind that if you want to use couple of such variables in same context you need to name it differently:$_
and$__
or$_1
.
Thanks, Vitaly.
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
- Recursion with multiple matches HOT 3
- Case as replacement for cond HOT 7
- 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.