Does the following
echo(vsum([[1,2],[3,4]]));
work for your definition?
-------- Original message --------
From: "David Eccles (gringer)" bioinformatics@gringene.org
Date:03/02/2015 17:24 (GMT-05:00)
To: discuss@lists.openscad.org
Cc:
Subject: Re: [OpenSCAD] A = A + 1;
I don't have a problem with OpenSCAD, because I treat it as a functional
language (with some recursion limitations), and everything is fine. There
are ways to implement variable modification in functional languages by
including a data store as an additional function parameter -- I'd have to
dig out my CS notes to find the way I was taught -- but it's frequently the
case that such a design is not necessary.
The 'concat' operator was, in my opinion, the final advance to make the
language essentially turing complete (and therefore fully expressive).
However, for proper use of functional languages, recursion is required in
many cases.
Thinking about the vector sum question, here's a recursive version that
basically uses assignment. The "cumSum" argument stores the current sum, and
the "pos" argument stores the current location in the vector:
function vsum(vec, pos = 0, cumSum = 0) =
pos >= len(vec) ? cumSum : vsum(vec, pos+1, cumSum + vec[pos]);
echo(vsum([1,2,3,8]));
--
View this message in context: http://forum.openscad.org/A-A-1-tp11385p11422.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
adriang wrote
Does the following
echo(vsum([[1,2],[3,4]]));
work for your definition?
I don't know any normal language that would automatically allow
vsum([1,2,3,8]) and vsum([[1,2],[3,4]]) into the same function and give a
sensible result. They have different types. One is a vector of integers, the
other is a vector of vector or integers. That's like applying an integer
function to a string and expecting it to magically figure out what you want.
Would you expect the result to be 10 or [4, 6]?
--
View this message in context: http://forum.openscad.org/A-A-1-tp11385p11434.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
On Wed, 4 Feb 2015 05:58:24 -0700 (MST)
Bananapeel lunatica.xiaoyu@gmail.com wrote:
adriang wrote
Does the following
echo(vsum([[1,2],[3,4]]));
work for your definition?
I don't know any normal language that would automatically allow
vsum([1,2,3,8]) and vsum([[1,2],[3,4]]) into the same function and give a
sensible result.
M, Haskell, Java, C++ templates, Smalltalk....
That's like applying an integer function to a string and expecting it to magically figure out what you want.
Which is what many languages do.
Would you expect the result to be 10 or [4, 6]?
What matters is that the function is defined sensibly and behaviour
defined for that type and to be useful.
Eg is the average of matrix of 3D co-ordinates more usefully a set of
vectors or a single point giving the "middle" (for that definition) of the
object ?
Alan
Here is a version that works with vectors and vectors of vectors:
function sum(v, i = 1) = i < len(v) ? v[i] + sum(v, i+1) : len(v) ? v[0] :
0;
echo(sum([]));
echo(sum([1,2,3,8]));
echo(sum([[1,2],[3,4]]));
ECHO: 0
ECHO: 14
ECHO: [4, 6]
On 4 February 2015 at 14:28, Alan Cox alan@lxorguk.ukuu.org.uk wrote:
On Wed, 4 Feb 2015 05:58:24 -0700 (MST)
Bananapeel lunatica.xiaoyu@gmail.com wrote:
adriang wrote
Does the following
echo(vsum([[1,2],[3,4]]));
work for your definition?
I don't know any normal language that would automatically allow
vsum([1,2,3,8]) and vsum([[1,2],[3,4]]) into the same function and give a
sensible result.
M, Haskell, Java, C++ templates, Smalltalk....
That's like applying an integer function to a string and expecting it to
magically figure out what you want.
Which is what many languages do.
Would you expect the result to be 10 or [4, 6]?
What matters is that the function is defined sensibly and behaviour
defined for that type and to be useful.
Eg is the average of matrix of 3D co-ordinates more usefully a set of
vectors or a single point giving the "middle" (for that definition) of the
object ?
Alan
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
adriang wrote
Does the following
echo(vsum([[1,2],[3,4]]));
work for your definition?
You should be able to validate that yourself. Everything that I wrote was
valid OpenSCAD code, and formed a complete instruction set. If you want a
specific answer, I could argue that yes it does work. The return result is
"undef", which is a perfectly reasonable return result for such a function
applied to a vector of vectors.
I wrote my function to fit roughly the same signature as that of Joymaker
(although admittedly that signature was for mean rather than sum). It's
obvious what is expected for a single vector of integers, but less obvious
when that vector includes other non-integer elements. It would be possible
for me to write a function that properly delves into the depths of the
vector and does something more useful, but its behaviour for that would
depend on the thoughts of the person who uses the function. I notice that R
will flatten any [simple] vectors when calculating means and sums:
a <- c(c(1,2,c(6,4)),c(3,4)); mean(a);
[1] 3.333333
But what about complex numbers? What if I want to put a function in as one
of the arguments? What about string values? What about oversized input? What
about blocking input? What about non-deterministic input? Sure, you can
provide answers to these, but designing a function to cover every
conceivable [mis]use is silly.
This is a nitpicky and offtopic rant which is largely unrelated to the issue
of how to name OpenSCAD variables, but perhaps at least demonstrates that
nuances of programming languages mean problems of interpretation can't
necessarily be fixed for everyone. I don't see how we can keep the
mathematical purists happy, while at the same time explaining things in a
"read once, understand everything" fashion, as well as keeping debate to a
minimum so that the bowls of petunias don't get annoyed with pointless
banter.
--
View this message in context: http://forum.openscad.org/A-A-1-tp11385p11456.html
Sent from the OpenSCAD mailing list archive at Nabble.com.