Re: Mathematica writing style

*To*: mathgroup at smc.vnet.net*Subject*: [mg18926] Re: Mathematica writing style*From*: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>*Date*: Tue, 27 Jul 1999 22:17:27 -0400*Organization*: Universitaet Leipzig*References*: <7ni8bb$5u7@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Hi, first of all, Mathematica is (deep in it's heart) a functional/logic programming language. No one writes large "programs" in a functional language. All people write many small functions in languages like LISP, Prolog .. The first thing you need is to split your huge function into many many small ones that is better for testing and writing. If you have this small functions tested well you will not miss the line numbers. You get line numbers when you load a package with Get[] and a syntax error is found. David Epstein wrote: > > It seems that Mathematica is great for writing small programs, > but things get difficult with larger programs because of lack of > modularity. For the most cases the lexical scoping of Module[] offer you the modularity you need. It seems that you don't have discovered packages and contexts. The context and the `Private` section of a context offer you all the modularity you can expect from a interpreter. > > In particular, there seems to be a problem when one makes a typo and the > mistake only emerges after the expression has been massaged by 20 > different functions. > > I was wondering about ending every list of definitions of a > function f, say, with something like > > f[x___] := error[f,x] > > and then producing a useful error with linenumber recorded using > $Line. The $Line variable helps you not because: what is the $Line number when you call in the 2456 "Input"-Cell a function defined in line 320 of a package you load ? > > I also thought of introducing some typing into the variables. Aaaargh. You introduce Pattern[] for function arguments. Mathematica has nothing that compare to a ordinary function arguments of a compiled language. A type of a variable is only needed by a compiler that has to reserve stack space for a function argument (ok, ok there are the Hold attributes). > One has a limited amount of typing provided by the system, like > f[x_List]. Could someone provide an example of how one might type > a more complicated structure without having to specify the head? I don't > fancy the idea of having a different head for each complicated > structure, but maybe that's the right thing to do. How compilcated do you like it f[x:{_Integer..}] f[x_?(VectorQ[#, IntegerQ] &)] this pattern will only match if the function argument is a vector of integers. And g[x_?(MatrixQ[#, IntegerQ] &)] /; Dimensions[x]=={1000,1000} := ... will only match a 1000 x 1000 matrix of integers or SomeTrafo[h:(Line |Polygon)[points_]] /; Length[points]==3 := will only match lines or polygons with three points ... You can still define your own tests and use the test to find the correct rule. It is a good idea to define your own heads like BinaryTree[cont_,left_,right_] instead of making {cont_,left_,right_}. > > Has anyone tried doing something like this? Is there some way > of doing the error function stuff automatically for every function that > one defines, > without having to do it explicitly? Mathematica is an interpreter. If you define a function like f[x_] it can't know that f[3] gives a result but f[2,5] is an error. You must tell Mathematica that f[] can only have one argument. When you define a function Mathematica can only check the syntax and not that you will missuse the function later in your session. What is with the nice Message[] function ? It is better to get "Expecting a integer for option Steps instead of blub." than "error in line 899123477, undefined symbol blub" > > Are there other measures one can take to stop errors propagating to > somewhere far from their source? I haven't actually tried out my > suggestion. Maybe there is some reason why it wouldn't work. Return[$Failed] ? is the usual method. Clearly your code has to test for the result and output some message. Hope that helps Jens