Can anyone explain a rational for this?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Can anyone explain a rational for this?

MichaelAtOz
Administrator
In doing a solution to Adrian's last post

> So in the case where if a is set then r=f(a), h=g(r,a) and if b is set
> then
> h=F(b), r=G(h,b), how would you write this?

I did this:

function f(i) = 2*i;
function F(j) = 3*j;
function g(k,l) = k*l;
function G(m,n) = m*n;

//a=5;
b=25;

aBased = !is_undef(a);
r = aBased
        ? f(a)
        : G(F(b),b);
h = aBased
        ? g(r,a)
        : F(b);


// without calling F(b) twice
aBased = !is_undef(a);
Fb = aBased ? 0 : F(b);

r = aBased
        ? f(a)
        : G(Fb,b);
h = aBased
        ? g(r,a)
        : Fb;

echo(r=r,h=h);

I added the second bit later, I was expecting the reassignment warnings, but
I get another problem.

> WARNING: aBased was assigned on line 9 of "Logo" but was overwritten on
> line 17
> WARNING: r was assigned on line 10 of "Logo" but was overwritten on line
> 20
> WARNING: h was assigned on line 13 of "Logo" but was overwritten on line
> 23
> Compiling design (CSG Tree generation)...
> WARNING: Ignoring unknown variable 'Fb', in file
> ../../Users/MeB/Documents/3D-REPRAP/Things/My Things/Logo, line 22.
> WARNING: Ignoring unknown variable 'Fb', in file
> ../../Users/MeB/Documents/3D-REPRAP/Things/My Things/Logo, line 25.
> ECHO: r = undef, h = undef

Why is Fb unknown?
It works if the top bit is commented out.
Side-effect of the new Warnings??

Using RC4.



-----
Admin - email* me if you need anything, or if I've done something stupid...

* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Admin - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Reply | Threaded
Open this post in threaded view
|

Re: Can anyone explain a rational for this?

nophead
It is the usual gotya that the last definition of a variable is used but its evaluation is moved to the where the first definition is. So it uses Fb before it has been defined.

On Sun, 28 Apr 2019 at 03:25, MichaelAtOz <[hidden email]> wrote:
In doing a solution to Adrian's last post

> So in the case where if a is set then r=f(a), h=g(r,a) and if b is set
> then
> h=F(b), r=G(h,b), how would you write this?

I did this:

function f(i) = 2*i;
function F(j) = 3*j;
function g(k,l) = k*l;
function G(m,n) = m*n;

//a=5;
b=25;

aBased = !is_undef(a);
r = aBased
        ? f(a)
        : G(F(b),b);
h = aBased
        ? g(r,a)
        : F(b);


// without calling F(b) twice
aBased = !is_undef(a);
Fb = aBased ? 0 : F(b);

r = aBased
        ? f(a)
        : G(Fb,b);
h = aBased
        ? g(r,a)
        : Fb;

echo(r=r,h=h);

I added the second bit later, I was expecting the reassignment warnings, but
I get another problem.

> WARNING: aBased was assigned on line 9 of "Logo" but was overwritten on
> line 17
> WARNING: r was assigned on line 10 of "Logo" but was overwritten on line
> 20
> WARNING: h was assigned on line 13 of "Logo" but was overwritten on line
> 23
> Compiling design (CSG Tree generation)...
> WARNING: Ignoring unknown variable 'Fb', in file
> ../../Users/MeB/Documents/3D-REPRAP/Things/My Things/Logo, line 22.
> WARNING: Ignoring unknown variable 'Fb', in file
> ../../Users/MeB/Documents/3D-REPRAP/Things/My Things/Logo, line 25.
> ECHO: r = undef, h = undef

Why is Fb unknown?
It works if the top bit is commented out.
Side-effect of the new Warnings??

Using RC4.



-----
Admin - email* me if you need anything, or if I've done something stupid...

* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Can anyone explain a rational for this?

MichaelAtOz
Administrator
Yea, thanks.
If others want to know what's going on see  here
<https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Include_Statement#Variables>
.



-----
Admin - email* me if you need anything, or if I've done something stupid...

* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Admin - email* me if you need anything,
or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.


The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!