From: drclark@daisy.uwaterloo.ca (David R. Clark)
Subject: Re: comparing CAS prog. languages
Organization: University of Waterloo, Waterloo, Ontario, Canada
Date: Sun, 22 Nov 1992 19:45:51 GMT


The following are Maple solutions to the functional programming problems posted
last week.  A couple of points: 

	1) I do not know Mathematica syntax at all so I hope I interpreted the 
	questions properly,

	2) I have avoided procedural solutions even when they would clarify the 
	code.

> run_encode := l -> if (l = []) then [] else [encode_block(l,1,1)] fi:
> encode_block := (l,i,c)-> if (i >= nops(l)) then [l[i],c] 
>                           elif (l[i] = l[i+1]) then encode_block(l,i+1,c+1); 
>                           else [l[i],c],encode_block(l,i+1,1); 
>                           fi:
> run_encode([a,a,a,b,b,b,c]);
                            [[a, 3], [b, 3], [c, 1]]

--------------------------------------------------------------------------------
> unnest1 := l->if type(l,list(list)) then op(map(unnest1,l)) else l fi:
> unnest := l -> [unnest1(l)]:
> unnest([[a,a],[[b,b,b],[b]],[c,c,c],[a]]);
                    [[a, a], [b, b, b], [b], [c, c, c], [a]]

--------------------------------------------------------------------------------
> frequency := run_encode@sort;
                          frequency := run_encode@sort

> frequency( [a,a,b,c,c,c,a]  );
                            [[a, 3], [b, 1], [c, 3]]

--------------------------------------------------------------------------------
> comparison_shop := (l1,l2) -> zip(min,l1,l2):
> comparison_shop([2,5,7,3],[3,2,8,2]);
                                  [2, 2, 7, 2]

--------------------------------------------------------------------------------
> bargain_hunter := (l1,l2,c) -> convert(zip((x,y) -> x*y,
>                                        comparison_shop(l1,l2),c),`+`):
> bargain_hunter([2,5,7,3],[3,2,8,2],[2,3,3,1]);
                                       33

--------------------------------------------------------------------------------
> sequences := n -> if (n = 0) then [] 
>                   elif (n = 1) then [[0],[1]] 
>                   else map(x->([op(x),0],[op(x),1]),sequences(n-1))
>                   fi:
> sequences(3);
 [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0],

     [1, 1, 1]]

--------------------------------------------------------------------------------
> josephus := (l,c) -> if (nops(l) = 1) then l 
>                      else josephus(subsop(c+1=NULL,l),c+1 mod (nops(l)-1)) 
>                      fi:
> survivor := proc(l) local i; josephus([i$i=1..l],1) end:
> survivor(10);
                                       [5]

--------------------------------------------------------------------------------
> split := (l,c)  ->  if l <> [] then  
>                        [[op(1..c[1],l)], 
>                          op(split([op((c[1]+1)..nops(l),l)], 
>                                   [op(2..nops(c),c)]))]
>                     else [] 
>                     fi:
> split([a,b,c,d,e,f],[2,0,3,1]);
                          [[a, b], [], [c, d, e], [f]]

--------------------------------------------------------------------------------
> random := i -> if (i <> 0) then irem(rand(),i) else 0 fi:
> deal3 := (l,c,i)->if (c>0) then 
>                      l[i+1], deal3(subsop(i+1=NULL,l),c-1,random(nops(l)-1));
>                   else NULL 
>                   fi :
> deal := (l,c) -> [deal3(l,c,random(nops(l)))]:
> card_dealing := (n,c) -> local i; deal([i$i=1..n],c):
> card_dealing(52,4);
                                [42, 18, 50, 22]

> suite := s -> s.(a,2,3,4,5,6,7,8,9,10,j,q,k):
> deck := [seq(suite(f),f={s,d,h,c})];
 deck := [ca, c2, c3, c4, c5, c6, c7, c8, c9, c10, cj, cq, ck, da, d2, d3, d4,

     d5, d6, d7, d8, d9, d10, dj, dq, dk, sa, s2, s3, s4, s5, s6, s7, s8, s9,

     s10, sj, sq, sk, ha, h2, h3, h4, h5, h6, h7, h8, h9, h10, hj, hq, hk]

> deal(deck,5);
                              [d2, d3, d8, c3, c2]

--------------------------------------------------------------------------------
> maxima := l -> if l = [] then [] else [l[1],maxima3(l,2,l[1])] fi:
> maxima3 := (l,i,x) -> if (i  > nops(l)) then  NULL 
>                       elif l[i] > x then l[i],maxima3(l,i+1,l[i]) 
>                       else maxima3(l,i+1,x) 
>                       fi :
> maxima([2,5,3,6,4,8,1]);
                                  [2, 5, 6, 8]

Newsgroups: sci.math.symbolic