Arved Sandstrom wrote: > I like the simplicity of Haskell's > filter pred list > or > [ f x | x <- list, pred x ] I've never gotten very far with Haskell, myself, so I don't know what that means. (The answer probably doesn't go in this forum, but if you could describe, in maybe 10 lines or less, what to download and install, and what to type in, to see the above in action, that would be a great post for jchat.) That said, I'm going to guess that in filter pred list, pred is either a selection vector or a selection function, and filter pred list somehow produces a new x where the pred selected items in x have been replaced by the result of applying a function inherent in filter to those items. (And the non-selected items remain unchanged and retain their relative positions in the result.) In J, filter=:adverb define : (u x#y) (I.x)} y ) i. 7 0 1 2 3 4 5 6 (1 0 1 0 1 0 1) (10*])filter i. 7 0 1 20 3 40 5 60 Or, if you know that y is numeric, you might instead use filtern=:adverb define : (y*-.x)+u y*x ) In the above example, filtern is a drop-in replacement for filter. Note that, in both cases, u must be a function which produces a result the same size as its argument. Typically, this means that each element in u y corresponds to a single element in y. Also note that in J, a predicate function probably best corresponds to a function which generates a boolean list (indicating what's selected and what's not). If you actually eliminate items from your argument, so your result is a different size from the argument, you wind up having to use something like e. to get the boolean list. As for [ f x | x <- list, pred x ], does this correspond to what you were thinking about? (7 8 9, 1 0 1 0 1 0 1#]) i. 7 7 8 9 0 2 4 6 Finally, a reminder: J is interpreted and has per-primitive overhead where in analogous contexts Haskell's compiler might eliminate overhead. However, in most contexts J's nouns can be thought of as having almost no cost (just the cost of the memory it takes to store them -- they're already computed). So algorithms expressed in terms of large regular nouns tend to work much better in J than algorithms expressed in terms of minute or irregular nouns.
Thanks, -- Raul
---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
