mini-rosetta

Table of Contents

These are all comparisons of J, haskell, and nial for now.

1 definitions

1.1 haskell

import Data.List (sort)
import Data.Function (on)

-- reflexive transform
rfx f y = f y y

-- monadic hook and fork
mfk f g h y = (f y) `g` (h y)
mhk = mfk id

1.2 nial

id is pass

% reflexive transform;
rfx is tr f op y {f y y }

% monadic hook and fork;
mhk is tr   g h op y {    y  g (h y) }
mfk is tr f g h op y { (f y) g (h y) }

square is (2 converse power)

2 running total of first 10 cardinals

+/\ >: i.10  NB. >: is 1+, '+/' is sum, '\' is scan
1 3 6 10 15 21 28 36 45 55
Prelude> scanl1 (+) [0..10]
[1,3,6,10,15,21,28,36,45,55]
accumulate + count 10
1 3 6 10 15 21 28 36 45 55

3 addition table for the numbers 0 1 2 3

+/~i.4
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
Prelude> let tbl f xs=[[f x y|x<-xs]|y<-xs]
Prelude> tbl(+)[0..3]
[[0,1,2,3],[1,2,3,4],[2,3,4,5],[3,4,5,6]]
rfx outer + 0 1 2 3
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6

4 repeat an operation n times

This is just doubling a number 5 times, which is the same as multiplying by 32, but it illustrates the concept:

+~^:5 [ 1  NB. +~ (reflexive addition) is also  +: (double) in j
32
Prelude> let pow = (\n f -> if n == 0 then id else f . pow (n-1) f)
Prelude> pow 5 (rfx (+)) 1
32
5 fold rfx + 1
32

5 three-verb forks (monadic)

(>: % +:) 2
0.75
Prelude> mfk (1+)(/)(2*) 2
0.75
/[1 +, 2 *] 2
0.75

6 check whether a series is strictly ascending

6.1 comparing the list to its sorted version

(-: /:~) i.10
1
Prelude Data.List> mhk (==) sort [0..9]
True
=[id,sortup] count 10
l

6.2 comparing (all but first item) to (all but last item)

*./ (}: < }.) i.10    NB. all (behead < curtail) i.10
1
Prelude> and $ (mfk tail (zipWith (<)) init) [0..9]
True
and <[front,rest] count 10
l

7 hooks (monadic)

Yet another way to double a number:

(+[) 5
10
Prelude> mhk (+) id 5
10
mhk[+,id] 5
10

8 apply a function to each argument, then a second function to the results

5 (+ & *:) 6   NB. '&' is 'atop' ... '*:' is square
61
Prelude Data.Function> on (+) (^2) 5 6
61
% '+' is already "binary pervasive";
+ square 5 6
61