Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Updates to hof script

From: jan:skibinski:sympatico:ca at: 16-Nov-2002 2:39

Hi Brett,
> Map-2 looks interesting. > > > Mapping two series via binary function: > > ((a -+ b -+ c) -+ [a] -+ [b] -+ [c]) > > I have to say though, the function signatures you include with your > functions always remind me of marks in the sand made by the feet of birds! > :^) >
Hi-hi, I had exactly the same impression when I was first introduced to tensor notation - with all those superscripts, subscripts - covariant, contravariant stuff. I got used to it and even became an expert.
> Seriously I've not understood one of them, and I did read your post where > you introduced them. Maybe there is a more accessible / more self-evident > notation for this? I don't think I would be the only one on this list that > has difficulty. > Mapping two series via binary function: > ((a -+ b -+ c) -+ [a] -+ [b] -+ [c])
I am sure there are better ways to explain this. But all I can do for now is to go through it again - but this time ignoring a type checker. Here are 4 groups of types: 3 of them represent arguments, the last one is a return type. The types here are generic: a, b, c - as generic as they can be, including any-type!. You can always substitute a generic type by a more concrete type, as in: a ==> decimal. But you need to be consistent and do it everywhere, changing the original pattern to something like that: ((decimal -+ b -+ c) -+ [decimal] -+ [b] -+ [c]) Of course 'b can be also 'decimal, so it can be 'c, as in: ((decimal -+ decimal -+ decimal) -+ [decimal] -+ [decimal] -+ [decimal]) Now the type is completely resolved to a certain specific case. First argument: (decimal -+ decimal -+ decimal) represents a function, that takes two arguments of type 'decimal and returns 'decimal. An example of such a function is :+. Square brackets signify series. I could be here more specific and try to distinguish between specific kind of series, such as block versus string, etc. But for a start, throwing everything into one "series" bucket should do. So reading the final version of this specific pattern: MAP-2 is a function that accepts a binary function of the type (decimal -+ decimal -+ decimal) and two series made of decimals. It produces a series of decimals as a result. Accordingly, the following application is legal: map-2 :+ [1 2 3] [1 1 1] == [2 3 4] Now, if you look again at the original definition:
> Mapping two series via binary function: > ((a -+ b -+ c) -+ [a] -+ [b] -+ [c])
it shows a lot of choices and a lot of potential for you to choose from. Let's think: what sort of functions, other than (decimal -+ decimal -+ decimal) could we use with map-2? Maybe some sort of comparison function? How about this one? map-2 :greater? [1 8 3] [4 5 6] If you run 'help on greater? you will find that its corresponding signature is: (any -+ any -+ any) But help does not tell the truth here! In fact, this should be: (ord -+ ord -+ logic) You can only compare things that are orderable, such as numbers, dates, times, strings, characters, but not points for example. You cannot say whether or not 1x4 is greater than 4x9. An obviously the result should be logic! - either true or false. But at least number of arguments is correct. So what does it do? map-2 :greater? [1 8 3] [4 5 6] == == [false true false] The point I am trying to make is that the patterns like this:
> ((a -+ b -+ c) -+ [a] -+ [b] -+ [c])
supposed to be self-documenting. They should also inspire you to think hard about possible reusability of such patterns. Hmm, what would this do? ((string -+ integer -+ string) -+ [string] -+ [integer] -+ [string]) OK, firstly I would need a binary function that takes a string and an integer and produces string. Name and index perhaps? A lookup function to some translation table? Perhaps indices could stand for some foreign language codes? 1 for Swedish, 2 for Czech perhaps? map-2 :lookup ["John" "Frank"] [1 3] == ["Jan", "Frantisek"] Perhaps I should post a separate script hof-examples.r with a lot of well documented examples. What do you think? All the best, Jan