Letter from Steve Gunhouse on DEF FN and FN

See all articles from Sinc-Link v8 n4
Author(s): Steve Gunhouse
Publication: Sinc-Link
Pub Details: Sinc-Link v8 n4 (Volume 8, Issue 4)
Date: July/August 1990
Pages: 8-9

[address omitted]
March 20, 1990

Dear George,

Being a mathematician, I tend to do a lot of fooling around with DEF FN and FN. You may have seen my letter on some of the stranger examples in ZX Computing about 3 years ago. Those weren’t exceedingly practical – they took too long to return an answer. But they were intended to illustrate just how much you could actually do with the DEF FN command. If written in the normal way, they would have taken several lines.

If you want a simpler (and more useful) function, try this one out:

1 DEF FN l$(a)=("          "+STR$ a)(LEN STR$ a TO)

This function produces a string of 11 characters which ends in the value of the variable a. In other words, it could be used for left-justifying numbers, if you want them to line up in nice columns. If you will be using relatively small numbers, you could drop a few of the spaces – just make sure there is one less than the final length you want.

I have in various places collections of dozens of DEF FN functions, though as I said most of them are not necessarily practical. Oh, if you are trying to figure out why a DEF FN might take a bit of time and can’t find the letter in question, it is because the functions involved something called recursion, in which a function may call itself several times in order to obtain an answer. Naturally, the more times the function has to call itself, the longer it takes. It is a sophisticated approach which lets you put the equivalent of a FOR/NEXT loop in a function, but it is somewhat tricky as well. Among other things, you have to design the function very carefully to avoid being caught in an endless loop.

I should give you an example. Some of the more sophisticated computers have a built-in function called INSTR. This function takes as parameters 2 strings and a number, and returns a number which is the first occurence of the second string in the first string, after the position corresponding to the number. If it isn’t found, a value of 0 is returned.

As a subroutine, such a function would look like the program below. Here, a$ is the first string, b$ is the second string, and s is the number input, and pos is the result.

1000 FOR i = s TO LEN a$-LEN b$+1
1010 IF a$(i TO i+LEN b$-1)=b$ THEN LET pos=i: GOTO 1030
1020 NEXT i: LET pos=0
1030 RETURN

That is actually quite a lot going on there. If it were written on another computer, I would have had to include one more IF to be sure s<=LEN a$-LEN b$+1, I would have to be sure a$ was long enough to possibly have b$ in it. On our computers, if s is too big, it will automatically skip the loop.

To do the same thing in a single DEF FN statement is complicated, of course, but it can be done. I have to specifically check all the possibilities. And I have to also use the VAL function to make sure I can get out of the loop. If I referred to the function directly, there would be no way out of the recursion.

So, without further ado, here it is:

5 DEF FN p(a$,b$,s)=VAL (VAL$ (("(""s"" AND a$(s TO s+LEN b$-1)=b$)+(""FN p(a$,b$,s+1)"" AND a$(s TO s+LEN b$-1)<>b$)") AND s<=LEN a$-LEN b$+1)+"+""""")+"+0")

As I said, it is extremely complicated. One thing that makes it especially complicated is that I have to make sure that if s>LEN a$-LEN b$+1 it will not attempt to evaluate any of the subscripted strings. The “+””””” and “+0” are there to avoid errors trying to evaluate an empty string. Let me try and walk through it for you.

There are 3 cases to consider. If s is too big, the whole thing collapses to VAL (VAL$(“+”””””)+”+0″). The VAL$ produces an empty string – it evaluates the string +”” – so that the VAL only evaluates +0 to get 0. That is as it is supposed to be. If s is small enough, then the VAL$ recieves the string “(“”s”” AND a$(s TO s+LEN b$-1)=b$)+(“”FN p(a$,b$,s)”” AND a$(s TO s+LEN b$-1)=b$)+”””””. Of course, the first and last quote are removed, and all the double quotes are converted to single quotes.

This is where the other two cases come in. We already know that s is small enough. If at that location, we can find b$ in a$, the VAL$ simply produces “s”, which has 0 added to it and is evaluated to give the answer. If b$ is not at that location, the VAL$ produces “FN p(a$,b$,s+1)”, which the VAL then evalutes to determine if b$ can be found at the next position (and the next, and the next, until we hit one of the other cases).

I admit, this is a rather extreme example, and hence will evaluate slowly if it can’t find the answer the place it checks. I presume if a$ was too long, it might actually produce an Expression too complicated error. Needless to say, that is not a problem with using an actual subroutine.

Anyway, enough fun for one day. That particular function I think I would never try and put to any practical use, but would instead use the subroutine. There are examples where the subroutine is less practical or almost impossible where this could be done, but they are unusual mathematical functions which might take too long to explain. Oh, needless to say, even the more sophisticated computers would have trouble handling.

I have not recieved the March issue of Sinc-Link yet, but I presume it is about as slow as usual and I will get it in the next couple of days. I’ll be mailing this from Windsor, where I am spending my March Break, so I won’t really know if I got it for a little while anyway. Take care, and Peace!

P.S., I just finished checking it out. I originally had one too many “(” in the DEF FN, but I have corrected it. And it performs as billed. If you were to use it to search a string of about 150 characters for the last character, it would take roughly a minute. Actually, that isn’t quite as bad as I expected, but it seems longer than it actually is.

Sincerely,
Steven V. Gunhouse