Re: [q-lang-users] Special values or failing functions
Brought to you by:
agraef
From: Tim H. <q...@st...> - 2007-05-17 08:19:44
|
Alexander Nickolsky <AN...@sp...> writes: [snip] > Then there comes another problem. Suppose I have a list of values > that I want to process using function like this, leaving only > results that make sense, that are 'proper' values. > It seems to me that I cannot make this procedure polymorphic, > because if for example 'false' means unwanted value for integer > function, 0 could mean unwanted value for boolean function, etc. > Another example is further processing of results. Say, I want > to find the longest path. It leads to the expression : > > if ((isnum P) and (isnum Q)) then (max P Q) else if (isnum P) then P > else if (isnum Q) then Q where P = (dist (A,B) N ), Q = (dist (C,D) N ); > > I do not think it's very readable. Wouldn't it be nice to have a > common 'special' value like nil or NULL or whatever, to indicate > failure ? Might () be appropriate? Without going too far into the specifics of your algorithm, the two things I would be doing are a) giving your expression a name - specifically, use more helper functions. You could invent "getmaxh" which takes all the (A,B) and (C,D) and dist (A,B) N and all that, which pushes the conditional checks down a level b) seek to use guards: write more rules for getmaxh that have the "when" on the left side: getmaxh (A,B) (C,D) () = (); getmaxh (A,B) (C,D) Z:Integer = foo; getmaxh (A,B) (C,D) Z = bar; //general case getmax (A,B) (C,D) = something to do with getmaxh introducing the Z parameter; As a bonus idea, can you seek to work with lists more, thereby allowing a filter (<>()) expression as a tidy-up phase (before or after getmax-ing)? ~Tim -- <http://spodzone.org.uk/> |