NEWTON

NEWTON


Popular tags

    What is the difference between tempvar/let in Cairo Lang? How to use allow_locals and local?

    Asked

    4 months ago

    104

    views


    1

    Hello! This is day 4 of the 17 days of the Cairo Challenge. And I have no idea how to solve the playground exercise “Variables”. Can you launch this code with the instructions and write down how many steps the program has in the output?

    [BONUS]: try to make it faster

    // The following code outputs the 9th power of 2.
    // 1. Click on "Debug" to run the code, and then click on
    //    the arrows in the memory panel to trace the program
    //    step by step.
    //
    // Observe that the computation of `z` take 8 steps and
    // creates 8 temporary variables. This is because `let`
    // simply replaces each instance of `y` with `x * x * x`
    // and each instance of `z` with `y * y * y`, which is
    // expanded to nine multiplications.
    //
    // 2. Change the two `let`s to `tempvar`s and run the program.
    //
    // "tempvar" stands for "temporary variable", which means that
    // a variable will be allocated for the intermediate values
    // (`x * x * x` and `y * y * y`). This reduces the same
    // computation to use only 4 steps and 2 new variables.
    //
    // 3. Replace all three instance of the keyword `tempvar`
    //    with `local`, uncomment the line containing `alloc_locals`
    //    and run the code again.
    //
    // The behavior of the code doesn't change. This is because
    // the keyword `local` allocates a new variables for the
    // intermediate values as well. The main difference between
    // `local` and `tempvar` is explained in the next challenge.
    
    // Use the output builtin.
    %builtins output
    
    // Import the serialize_word() function.
    from starkware.cairo.common.serialize import serialize_word
    
    // Outputs the value 2^9.
    func main{output_ptr: felt*}() {
        // alloc_locals;
        tempvar x = 2;
        let y = x * x * x;
        let z = y * y * y;
        // Because of the `let`s, the following line is equivalent to
        // serialize_word(x * x * x * x * x * x * x * x * x).
        serialize_word(z);
        return ();
    }
    

    Answers to this question are a part of the ✨ 17 days of Cairo Lang with Playground & Newton. ✨

    Vote for your favorite answer - the best answer will win a $10 award. A new day – a new reward! During the next 17 days, our goal is to attract more developers to the Cairo language and to systematize the knowledge of Cairo lang. Read rules

      #17daysOfCairocairo-beginnerscairoplayground

    Newton

    asked

    4 months ago


    4 answers

    2

    Accepted answer

    Challenge to see the difference between let, tempvar and local in a Cairo contract.

    Docs to learn more.

    func main{output_ptr: felt*}() {
        alloc_locals;
        local x = 2;
        local y = x * x * x;
        local z = y * y * y;
    
        serialize_word(z);
        return ();
    }
    

    Ivan

    answered

    4 months ago

    1

    // Use the output builtin. %builtins output

    // Import the serialize_word() function. from starkware.cairo.common.serialize import serialize_word

    // Outputs the value 2^9. func main{output_ptr: felt*}() { alloc_locals; local x = 2; local y = x * x * x; local z = y * y * y; // Because of the lets, the following line is equivalent to // serialize_word(x * x * x * x * x * x * x * x * x). serialize_word(z); return (); }

    It decreased from having 15 steps, to 13 steps(even adding the "alloc_locals" step)

    answered

    4 months ago

    0

    Solution

    %builtins output
    
    // Import the serialize_word() function.
    from starkware.cairo.common.serialize import serialize_word
    
    // Outputs the value 2^9.
    func main{output_ptr: felt*}() {
        // alloc_locals;
        let x = 2;
        let y = x * x * x;
        let z = y * y * y;
        // Because of the `let`s, the following line is equivalent to
        // serialize_word(x * x * x * x * x * x * x * x * x).
        serialize_word(z);
        return ();
    }
    

    Output

    Program output:
      512
    Number of steps: 7
    Program hash: 0x01369567b8bbbd05c7bd71fc0f26943286f30246277672185a082e4c5d5476bb
    

    Your answer

    NEWTON

    NEWTON