Functional programming

Chris Angelico rosuav at gmail.com
Mon Mar 3 15:23:01 CET 2014


On Tue, Mar 4, 2014 at 1:08 AM, Rustom Mody <rustompmody at gmail.com> wrote:
>> How do you know that [1,2] is a list that must contain nothing but
>> integers? By extension, it's also a list that must contain positive
>> integers less than three, so adding [5] violates that. And [] is a
>> list that must contain nothing, ergo it can't be added to, although
>> (since it contains nothing) it can be added to anything.
>
> If 'integer-less-than-3' were a type then yes there would be this
> problem. More generally, if types could overlap then automatic
> type-inference is impossible
>

First, does Haskell allow this?

? [1,2,'foo'] ++ [3,4,'bar']

If not, then find some other form of the expression that has the same
point, and substitute in for the below. And then: which of these is
permitted?

? [1,2] ++ [3,4,'bar']
? [1,2,'foo'] ++ [3,4]
? [] ++ [3,4,'bar']
? [1,2,'foo'] ++ []
? ([1,2,'foo'] ++ []) ++ [3,4,'bar']
? [1,2,'foo'] ++ ([] ++ [3,4,'bar'])

If it's okay to have heterogeneous lists, how do you tell it that your
[1,2] is actually going to get strings in it later, and that it's okay
to combine it with one that has strings?

With Pike, that's all based on the variable type. (There is type
inference; the type of an array containing just 3 and 4 is
"array(int(3..4))", but it's acceptable to add that to an array
containing the string "asdf", which itself has type
"array(string(97..115))" - the combination would be "array(int(3..4) |
string(97..115))", which gets a bit wordy.) I can't assign an
array(string) to a variable that's been declared as taking array(int).
But I can assign an array(int) to a variable declared as accepting
array(int|string), and then I can append a string to it, because
that's legal based on the destination.

ChrisA



More information about the Python-list mailing list