Re: A tiny bit more about structures and arrays

new topic     » goto parent     » topic index » view thread      » older message » newer message

At 06:46 p.m. 28-01-99 -0800, you wrote:

>Would someone who understands both Euphoria & Pascal please convert the
>following to Euphoria 2.0 syntax. I suspect it will be quite a bit of code
>so its a big favor I know. Also, I apologize for the compressed Pascal
>syntax in advance...

Uncompressed pascal:

type
        t_sex = (m,f);
        t_digit = ("0".."9");
        t_ssn = packed array [1..9] of t_digit;
        t_name = string[30];
        t_date = record
                year : 1900..2100;
                month : 1..12;
                day : 1..31;
        end;
        t_person = record
                who , title : t_name;
                ident : t_ssn;
                gender : t_sex;
                born , hired : t_date;
        end;

Euphoria "equivalent":

type t_sex (integer sex)
        return (sex = 'm') or (sex = 'f')
end type

type t_digit (integer digit)
        return (digit >= "0") and (digit <= "9")
end type

type t_ssn (sequence ssn)
        if length(ssn) > 9 then
                return 0
        else if length(ssn) = 0 then
                return 1
        else
                for i = 1 to length(ssn) do
                        if not t_digit(ssn[i]) then
                                return 0
                        end if
                end for
        end if
        return 1
end type

type t_name (sequence name)
        if length(name) > 30 then
                return 0
        else if length(name) = 0 then
                return 1
        else
                for i = 1 to length(ssn) do
                        if (name[i] < 0) or (name[i] > 254) then
                                return 0
                        end if
                end for
        end if
end type

type t_date (sequence date)
        if length(date) = 0 then
                return 1
        else if length(date) != 3 then
                return 0
        else
                if (date[1] < 1900) or (date[i] > 2100) then
                        return 0
                end if
                if (date[2] < 1) or (date[2] > 12) then
                        return 0
                end if
                if (date[3] < 1) or (date[3] > 31) then
                        return 0
                end if
        end if
        return 1
end type

type t_person (sequence person)
        return t_name(person[1]) and t_name(person[2]) and t_ssn(person[3]) and
t_sex(person[4]) and t_date(person[5]) and t_date(person[6])
end type

>I work with records / structures a **lot** in other languages and I think
>that I can explain Tor's questions if I have a good example of how the code
>might be simplified from 2.0 to a fictional/proposed Euphoria with
>structures (Euphoria 3.0 ?).

As you can see it's very dificult to "strucutrize" Euphoria. Euphoria, on
it's language definition, it very struturized (and I love that), but data
types aren't designed that way :(

If you want to create a t_person "array" you'll need another type declaration:

type t_person_list (sequence person_list)
        if length(person_list) = 0 then
                return 1
        end if
        for i = 1 to length(person_list)
                if not t_person(person_list[i]) then
                        return 0
                end if
        end for
        return 1
end type


Regards,
        Daniel   Berstein
        daber at pair.com

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu