home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / sci / math / symbolic / 3075 < prev    next >
Encoding:
Text File  |  1992-11-22  |  4.7 KB  |  114 lines

  1. Newsgroups: sci.math.symbolic
  2. Path: sparky!uunet!utcsri!torn!watserv2.uwaterloo.ca!watdragon.uwaterloo.ca!watdragon!drclark
  3. From: drclark@daisy.uwaterloo.ca (David R. Clark)
  4. Subject: Re: comparing CAS prog. languages
  5. In-Reply-To: Richard J. Gaylord's message of Wed, 18 Nov 1992 14:24:16 GMT
  6. Message-ID: <DRCLARK.92Nov22144551@daisy.uwaterloo.ca>
  7. Sender: news@watdragon.uwaterloo.ca (USENET News System)
  8. Organization: University of Waterloo, Waterloo, Ontario, Canada
  9. References: <Bxx1CI.Fzv@news.cso.uiuc.edu>
  10. Date: Sun, 22 Nov 1992 19:45:51 GMT
  11. Lines: 101
  12.  
  13.  
  14. The following are Maple solutions to the functional programming problems posted
  15. last week.  A couple of points: 
  16.  
  17.     1) I do not know Mathematica syntax at all so I hope I interpreted the 
  18.     questions properly,
  19.  
  20.     2) I have avoided procedural solutions even when they would clarify the 
  21.     code.
  22.  
  23. > run_encode := l -> if (l = []) then [] else [encode_block(l,1,1)] fi:
  24. > encode_block := (l,i,c)-> if (i >= nops(l)) then [l[i],c] 
  25. >                           elif (l[i] = l[i+1]) then encode_block(l,i+1,c+1); 
  26. >                           else [l[i],c],encode_block(l,i+1,1); 
  27. >                           fi:
  28. > run_encode([a,a,a,b,b,b,c]);
  29.                             [[a, 3], [b, 3], [c, 1]]
  30.  
  31. --------------------------------------------------------------------------------
  32. > unnest1 := l->if type(l,list(list)) then op(map(unnest1,l)) else l fi:
  33. > unnest := l -> [unnest1(l)]:
  34. > unnest([[a,a],[[b,b,b],[b]],[c,c,c],[a]]);
  35.                     [[a, a], [b, b, b], [b], [c, c, c], [a]]
  36.  
  37. --------------------------------------------------------------------------------
  38. > frequency := run_encode@sort;
  39.                           frequency := run_encode@sort
  40.  
  41. > frequency( [a,a,b,c,c,c,a]  );
  42.                             [[a, 3], [b, 1], [c, 3]]
  43.  
  44. --------------------------------------------------------------------------------
  45. > comparison_shop := (l1,l2) -> zip(min,l1,l2):
  46. > comparison_shop([2,5,7,3],[3,2,8,2]);
  47.                                   [2, 2, 7, 2]
  48.  
  49. --------------------------------------------------------------------------------
  50. > bargain_hunter := (l1,l2,c) -> convert(zip((x,y) -> x*y,
  51. >                                        comparison_shop(l1,l2),c),`+`):
  52. > bargain_hunter([2,5,7,3],[3,2,8,2],[2,3,3,1]);
  53.                                        33
  54.  
  55. --------------------------------------------------------------------------------
  56. > sequences := n -> if (n = 0) then [] 
  57. >                   elif (n = 1) then [[0],[1]] 
  58. >                   else map(x->([op(x),0],[op(x),1]),sequences(n-1))
  59. >                   fi:
  60. > sequences(3);
  61.  [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0],
  62.  
  63.      [1, 1, 1]]
  64.  
  65. --------------------------------------------------------------------------------
  66. > josephus := (l,c) -> if (nops(l) = 1) then l 
  67. >                      else josephus(subsop(c+1=NULL,l),c+1 mod (nops(l)-1)) 
  68. >                      fi:
  69. > survivor := proc(l) local i; josephus([i$i=1..l],1) end:
  70. > survivor(10);
  71.                                        [5]
  72.  
  73. --------------------------------------------------------------------------------
  74. > split := (l,c)  ->  if l <> [] then  
  75. >                        [[op(1..c[1],l)], 
  76. >                          op(split([op((c[1]+1)..nops(l),l)], 
  77. >                                   [op(2..nops(c),c)]))]
  78. >                     else [] 
  79. >                     fi:
  80. > split([a,b,c,d,e,f],[2,0,3,1]);
  81.                           [[a, b], [], [c, d, e], [f]]
  82.  
  83. --------------------------------------------------------------------------------
  84. > random := i -> if (i <> 0) then irem(rand(),i) else 0 fi:
  85. > deal3 := (l,c,i)->if (c>0) then 
  86. >                      l[i+1], deal3(subsop(i+1=NULL,l),c-1,random(nops(l)-1));
  87. >                   else NULL 
  88. >                   fi :
  89. > deal := (l,c) -> [deal3(l,c,random(nops(l)))]:
  90. > card_dealing := (n,c) -> local i; deal([i$i=1..n],c):
  91. > card_dealing(52,4);
  92.                                 [42, 18, 50, 22]
  93.  
  94. > suite := s -> s.(a,2,3,4,5,6,7,8,9,10,j,q,k):
  95. > deck := [seq(suite(f),f={s,d,h,c})];
  96.  deck := [ca, c2, c3, c4, c5, c6, c7, c8, c9, c10, cj, cq, ck, da, d2, d3, d4,
  97.  
  98.      d5, d6, d7, d8, d9, d10, dj, dq, dk, sa, s2, s3, s4, s5, s6, s7, s8, s9,
  99.  
  100.      s10, sj, sq, sk, ha, h2, h3, h4, h5, h6, h7, h8, h9, h10, hj, hq, hk]
  101.  
  102. > deal(deck,5);
  103.                               [d2, d3, d8, c3, c2]
  104.  
  105. --------------------------------------------------------------------------------
  106. > maxima := l -> if l = [] then [] else [l[1],maxima3(l,2,l[1])] fi:
  107. > maxima3 := (l,i,x) -> if (i  > nops(l)) then  NULL 
  108. >                       elif l[i] > x then l[i],maxima3(l,i+1,l[i]) 
  109. >                       else maxima3(l,i+1,x) 
  110. >                       fi :
  111. > maxima([2,5,3,6,4,8,1]);
  112.                                   [2, 5, 6, 8]
  113.  
  114.