Raul Miller-4 wrote:
>
> On Thu, Apr 7, 2011 at 12:42 PM, Aai <[email protected]> wrote:
>> Sorry, should have sent it also to chatforum.
>>
>> Whatever complex initial argument you want to start with, applying f on
>> initial
>> element and an element of the list should be on the same types, e.g.
>>
>> Prelude> foldl (\(x,y) e -> (y,x+e)) (0,2) [1..5]
>> (8,9)
>>
>> Here the initial element argument is typed
>> (a,a)
>> and the list is typed
>> [a]
>>
>> The result is typed
>> (a,a)
>>
>> Note: a means same type.
>>
>> If not, I do not follow you. Please give an example.
>
>
> My point was that the initial element and result have a different type
> from the type of an element of the list.
>
> In other words: type of initial argument and of result:
>
> (a,a)
>
> type of an element of the list:
>
> a
>
In Haskell, every function has some well defined type.
Type of foldr, for instance, is defined as:
foldr :: (a -> b -> b) -> b -> [a] -> b
There are three outer arrows here,
so we can read that foldr takes three arguments:
i) something of type a->b->b, i.e a function that takes
two arguments of type a and b and returns something
of type b. If a and b are the same concrete type,
like Integer, then the function that is the first argument
of foldr must return also Integer. Otherwise, if function
takes say Integer and String, it must return String--if it
returns [String] in its stead, we get a type mismatch error
if we try to use it with foldr.
ii) something of type b
iii) something of type [b], which is just a list of elements
of type b.
This kind of typology is essential to get the things right in Haskell.
Now,
foldr1 :: (a -> a -> a) -> [a] -> a
and that's where analogy between foldr1 and / falls through,
because in J not only that we don't have to have all the
elements of the argument y of u/ to be of the same type,
we don't even have to have the return type of u to be the
same as that of the elements of y, or, to paraphrase Roger,
Reduce is a poor name for / because / does not necessarily
reduce anything.
> In contrast, in J, the initial element is the rightmost element of the
> list, which means it must be the same type as the rest of the list.
>
Not really. Let's consider how ,./i. 3 3 works:
,./i. 3 3 === 0 1 2 ,. 3 4 5 ,. 6 7 8
The right ,. has two arguments of rank 1 and returns
the result of rank 2, namely:
3 6
4 7
5 8
Thus, the left ,. has the left argument of rank 1
and the right argument of rank 2.
> This means that we must use something (what a haskell programmer would
> call a monad) to have this initial element be a different logical type
> from the rest of the list.
>
> Do you disagree with this assessment? If so, can you give a
> counter-example?
>
>
> Or, if you agree, does my initial post in this thread make more sense
> now? Or do you see flaws in it, also?
>
> Thanks,
>
> --
> Raul
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
>
--
View this message in context:
http://old.nabble.com/the-design-of--%5C-tp31343248s24193p31347101.html
Sent from the J Chat mailing list archive at Nabble.com.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm