Pairs (was It's a list!...) (was Function Newbie...)
Dar Scott
dsc at swcp.com
Sat Dec 7 19:54:00 EST 2002
On Friday, December 6, 2002, at 10:08 PM, Dar Scott wrote:
> I'm working on a way to pack some values together and take them apart.
On Saturday, December 7, 2002, at 03:15 AM, Malte Brill wrote:
> Hi Dar, I defenitly want to learn more about recursion. Are there any
> tutorials on the web, that could help me getting startet?
Here are my simple functions that inspired my alternative to lists, be
they boxes, nodes or whatever. They are much simpler than what I'm
working on now and in many ways are better.
They simply form a pair from two values and extract the values. This is
the basis of lisp lists. From this can be built all kinds of structures
and functions to handle them.
Traditionally, functions built upon pairs are recursive.
The values for pairing can be any strings including binary strings. No
arrays. Numbers may lose something. Pairing can be nested, of course,
and especially stringed together to make "lists". Put empty into the
right side of the last pair when making a "list".
Have fun!
*****************
-- Rev 1.1.1 OS X 10.15
-- Pairs
-- Dar Scott thunked this up 2002
--
-- Make a pair with P(a,b), extract with L(p)-->a, R(p)-->b
-- create a pair from two strings, the left and the right
function P A B
local lenBytes
put A & empty into A
put binaryEncode("N",length(A)) into lenBytes
return lenBytes & A & B
end P
-- get the left side of a pair
function L thePair
local lenBytes, lenNum, numConverted
put char 1 to 4 of thePair into lenBytes
put binaryDecode("N",lenBytes,lenNum) into numConverted
if numConverted is not 1 then return empty
return char 5 to (lenNum+4) of thePair
end L
--get the right side of a pair
function R thePair
local lenBytes, lenNum, numConverted
put char 1 to 4 of thePair into lenBytes
put binaryDecode("N",lenBytes,lenNum) into numConverted
if numConverted is not 1 then return empty
return char (lenNum+5) to -1 of thePair
end R
*******************
Dar Scott
More information about the use-livecode
mailing list