[q-lang-users] a little question
Brought to you by:
agraef
From: Rob H. <hub...@gm...> - 2006-08-25 12:12:25
|
Hello Fabian, As you seem to have noticed, the rule add [X|Xs] = X+add Xs; does not work for the list []. This is because [] does not match the pattern [X|Xs]. The pattern [X|Xs] is only matched by non-empty lists. Q has not been told what to do with "add []". Your modification fails to solve the problem for a similar reason. If you tell Q what to do with the following rule add [] = 0; then all will be well. Alternatively, if you're not happy with defining add on the empty list, you could do this: add [X] = X; add [X|Xs] = X+add Xs; Rob. On 25/08/06, Fabian Boucsein <kan...@we...> wrote: > hello, > > from the q manual i tried the following example: > > add [X|Xs] = X+add Xs; > > then i tried this to test the function: > > ==> add [1, 2, 3, 4] > 1+(2+(3+(4+add []))) > > what i want to know is, how can i change this function to get > back a result? i tried the following: > > add [X|Xs] = X+add Xs if Xs <> []; > > ==> add [1, 2, 3, 4] > 1+(2+(3+add [4])) > > which is still not the result i want. by the way i am using q 7.3 > maybe there is someone who can help me. > > greetings, > > fabian |