On Wed, 4 Feb 2015 05:58:24 -0700 (MST)
Bananapeel <[hidden email]> 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 [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
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 <[hidden email]> wrote: On Wed, 4 Feb 2015 05:58:24 -0700 (MST) _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
In reply to this post by Lampbus
In a functional programming language, a 'constant' is an expression
like '42' which always has the same value, in every run of the program. A 'variable' is a named value that can have different values in different runs of the program. Even if you have a top level definition like x = 0; this can still vary, in OpenSCAD, because you can override the value '0' in several ways, eg by invoking openscad with -Dx=1. Like Bananapeel said, OpenSCAD is a functional programming language. There are many others, such as Haskell, Ocaml, F#, ML, Erlang, and so on. The word 'variable' is used in all of these languages to describe what OpenSCAD calls a variable. And there isn't really a good alternative to the word 'variable'. So we need to do what Bananapeel said: fix the documentation and fix the error reporting so that OpenSCAD can be more easily understood by people coming from an imperative programming background. On 4 February 2015 at 08:34, Richard Benjamin <[hidden email]> wrote: > Hmm, I am no expert, but in your example below, 'a' is not varying, it is > constant - it gets calculated once and set up for use. > > It would only be a variable if it varies during the program run, eg if 'b' > changes at certain points in time....but openscad never 'runs' it just puts > the STL together, and 'b' is always the same. > > > On 04/02/2015 13:03, Bananapeel wrote: >> >> Every other language without destructive update operator (c-style assign) >> calls them variables, I don't see why OpenSCAD should be different. >> >> They aren't "constant", because a constant is a value that never changes. >> function square(s) = s*s; >> b = 6; >> a = square(b); >> The variable a's value isn't constant - it changes according to the value >> of >> b. >> >> What's really needed is: >> * Giving an error message when trying to redefine a variable. >> * Clean up other doubtful cases. >> * An explanation for people from procedural programming languages >> (C-style). >> * Push all these changes to release version. >> >> Have a nice day, >> Bananapeel :) >> >> >> >> >> -- >> View this message in context: >> http://forum.openscad.org/A-A-1-tp11385p11436.html >> Sent from the OpenSCAD mailing list archive at Nabble.com. >> >> _______________________________________________ >> 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 > _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
In reply to this post by adriang
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. |
Free forum by Nabble | Edit this page |