Git Product home page Git Product logo

Comments (6)

vsbogd avatar vsbogd commented on August 14, 2024

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.

Necr0x0Der avatar Necr0x0Der commented on August 14, 2024

Apparently, nested ifs 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 ifs 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.

DaddyWesker avatar DaddyWesker commented on August 14, 2024

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.

DaddyWesker avatar DaddyWesker commented on August 14, 2024

@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.

vsbogd avatar vsbogd commented on August 14, 2024

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.

DaddyWesker avatar DaddyWesker commented on August 14, 2024

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)

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.