2009/10/2 Erling Hellenäs <[email protected]>:
> ... When I work in Haskell I miss the
> efficient notation of J and APL though. It seems you could
> create something like J as a compiled language written in
> Haskell or as an extension to Haskell. Anyone gave it a try
> or have related ideas ?

I like to consider J as a mariage of two relatively independent
languages: one for array manipulation and one for functional
programming.  When programming in Haskell, I sometimes tend to
miss some of the ways functions can be composed in J.  So I
imitate J in Haskell.  The data models of the two languages are
vastly different, but as long as functional programming is
concerned, it is possible to add some J to Haskell.

For example, I can define:

psi f g x y = f (g x) (g y)
( or, if you prefer: psi f g x y = (g x) `f` (g y) )

which is analogous to J's dyadic &, a.k.a. the ‘Ψ-combinator’
elsewhere.  In turn, this makes it possible to define, e.g.:

comparing = psi compare

( assuming the type for the function comparing:
comparing :: Ord a => (b -> a) -> b -> b -> Ordering .)

Now, in order to sort a list of values according to their
projections under function f, I can use the following function:

sortBy $ comparing f

E.g.,

(sortBy $ comparing snd) [(2,1),(3,0),(4,10),(5,-2),(6,4)]

returns

[(5,-2),(3,0),(2,1),(6,4),(4,10)]

If I also define this combinator:

o f g x y = f (g x y)

(analogous to J's dyadic @), and then

f1   = psi (==) abs
f1'  = psi (<) abs
f2   = psi (abs`o`(-)) abs
f3   = psi (+) (^2)
f3'  = psi (sqrt`o`(+)) (^2)
f3'' = foldr1 f3'

here is what each of the six functions computes:

f1    |a|=|b|
f1'   |a|<|b|
f2    ||a|-|b||
f3    a²+b²
f3'   √(a²+b²)
f3''  √(a²+b²+…)

You can see that borrowing a bit of J fits Haskell pretty well.
I wouldn't go too far in the way of interpreting J in Haskell,
though.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to