Roger,
I regret I started this, because if you take this up
as a kind of personal challenge against Haskell than
this was not what I had in mind. If you want a worthy opponent
I'm of no match (knowledge, experience) to your expertise
and knowledge. I did take up programming in Haskell only
a year ago, so I'm not an experienced programmer in Haskell.
Still learning almost every day, same thing with J.
Another thing is I have to get used to formulate things
in English and that's not my daily cup of tea (not to use
an excuse though).
> a. You did not indicate what the J time-space numbers
> were on the expressions.
>
ts 'i4'
6e_6 448
ts 'i9'
5e_6 448
To compare with Haskell I'll have to deal with, as
Oleg Kobchenko mentioned, lazy evaluation. To have
access to values in memory I have to come up with
array's (in memory values) instead of list values
which are only created when you need them. You only
define the type and size (incl. infinity) of the list.
f.e.
list :: Int -> [Integer]
list n = [1..n]
or
list = [1..] -- infinite list
Here is a summation result with a list of 1e6
random values:
*Main> main
Computation time: 4.192 sec
ok.
(4.19 secs, 1183297592 bytes)
First timing is an attempt to time just the the summation.
Second timing is the total time of running the program.
As you can see: no difference, meaning time is inclusive
the creation of random values (as they are being accessed then).
With a trick (using the list values more than once) I can
come up with:
*Main> main
Computation time: 4.944 sec I
Computation time: 0.192 sec II
ok
(5.14 secs, 1270145980 bytes)
Time I is inclusive the creation of random values
during the summation.
Time II: values exist already, so this value is the exclusive
summation time.
> b. +/i.n is not a good test as 2!n would give an
> instantaneous answer. Please do the benchmarks
> on random numbers.
>
Here's another nice thing of J: Combinations (2,n).
Build in in J (probably highly optimized).
To compare with Haskell I'll have to write some
functions for that. So a fair comparison would be if
I do the same in J.
Used functions in J:
(not using the build in functions)
fac=: 3 : 'if. y=0 do. 1 else. */ 1+ i.y end.'
comb=: 4 : '(fac y) % (fac x) * fac y-x'
ts 'it=:2 comb 5000x'
0.625806 882816
it
12497500
but:
ts '2!5000x'
5.4e_5 167552
Same in Haskell:
Straightforward (no clever things)
fac 0 = 1
fac n = foldl (*) 1 [1..n]
comb k n = fac n `div` (fac k * fac (n-k))
note: I don't use a strict fold here nor
the build in function 'product'
main = do
time $ comb 2 5000 `seq` return ()
print $ comb 2 5000
*Main> main
Computation time: 0.204 sec
12497500
(0.41 secs, 93522164 bytes)
Hopefully this will satisfy ;-)
Again: I'm not promoting Haskell, I 'love' J as much!
Since I know about Haskell and J I feel like a child in Legoland!
Thanks
@@i=arie
p.s.
keep in mind the time zone difference in my responding
to your e-mails.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm