Ignore:
Timestamp:
Oct 24, 2008, 1:46:09 PM (12 years ago)
Author:
yura
Message:
  • Rewritten.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/java/refal/refal/plus/List2.rf

    r3993 r3994  
    55$use Apply Access Arithm Compare;
    66
    7 $func? Apply t.func e.arg = e.res;
    8 Apply {
    9   s.func e.arg = <Apply.Apply s.func e.arg>;
    10   (s.func e.arg1) e.arg2 = <Apply.Apply s.func e.arg1 e.arg2>;
    11 };
     7$func SimplifyFunction t.func e.arg = s.func e.arg;
     8SimplifyFunction t.func e.arg =
     9  t.func e.arg $iter {
     10    t.func : (t.func1 e.arg1) = t.func1 e.arg1 e.arg;
     11  } :: t.func e.arg, t.func : s.funcRef =
     12  s.funcRef;
    1213
    13 $func PApply t.func e.arg = t.func;
    14 PApply {
    15   s.func = s.func;
    16   s.func e.arg = (s.func e.arg);
    17   (s.func e.arg1) e.arg2 = (s.func e.arg1 e.arg2);
    18 };
     14$public $func? Apply t.func e.arg = e.res;
     15Apply t.func e.arg = <Apply.Apply <SimplifyFunction t.func e.arg>>;
    1916
     17$public $func PApply t.func e.arg = t.func;
     18PApply t.func e.arg = <SimplifyFunction t.func e.arg> :: s.func e.arg,
     19  {
     20    e.arg : /* empty */ = s.func;
     21    = (s.func e.arg);
     22  };
    2023
    21 $func Map t.func e.list = e.list;
     24$public $func Map t.func e.list = e.list;
    2225Map t.func e.list =
    2326  () e.list $iter {
    24     e.list : t.item e.rest = (e.new_list <Apply t.func t.item>) e.rest;
    25   } :: (e.new_list) e.list,
    26   e.list : /*empty*/ =
     27    e.list : t.head e.tail = (e.new_list <Apply t.func t.head>) e.tail;
     28  } :: (e.new_list) e.list, e.list : /*empty*/ =
    2729  e.new_list;
    2830
     
    3436
    3537$func FilterHelp t.func t.term = e.term_or_empty;
    36 FilterHelp t.func t.term = { <Apply t.func t.term> : e = t.term; ; };
     38FilterHelp t.func t.term = { <Apply t.func t.term> :: e = t.term; ; };
    3739
    3840$public $func Filter t.func e.list = e.list;
    3941Filter t.func e.list = <Map (&FilterHelp t.func) e.list>;
    4042
    41 Split s.Fname e.Fargs (e.list) =
     43$public $func Split t.func e.list = (e.true_list) (e.false_list);
     44Split t.func e.list =
    4245  () () e.list $iter {
    43     e.list : t.item e.rest, {
    44       <Apply s.Fname e.Fargs t.item> : e =
    45         (e.true_list t.item) (e.false_list) e.rest;
    46       (e.true_list) (e.false_list t.item) e.rest;
     46    e.list : t.head e.tail, {
     47      <Apply t.func t.head> :: e = (e.true_list t.head) (e.false_list) e.tail;
     48      = (e.true_list) (e.false_list t.head) e.tail;
    4749    };
    48   } :: (e.true_list) (e.false_list) e.list,
    49   e.list : /*empty*/ =
     50  } :: (e.true_list) (e.false_list) e.list, e.list : /*empty*/ =
    5051  (e.true_list) (e.false_list);
    5152
     
    5354IsElem e t.item e t.item;
    5455
    55 $func Foldl t.func (e.value) e.list = e.value;
     56$public $func Foldl t.func (e.value) e.list = e.value;
    5657Foldl t.func (e.value) e.list =
    5758  (e.value) e.list $iter {
    58     e.list : t.head e.tail =
    59       (<Apply t.func e.value t.head>) e.tail;
    60   } :: (e.value) e.list,
    61   e.list : /*empty*/ =
     59    e.list : t.head e.tail = (<Apply t.func e.value t.head>) e.tail;
     60  } :: (e.value) e.list, e.list : /*empty*/ =
    6261  e.value;
    6362
    64 $func Foldl1 t.func v.list = e.value;
     63$public $func Foldl1 t.func v.list = e.value;
    6564Foldl1 t.func t.head e.tail = <Foldr t.func (t.head) e.tail>;
    6665
    67 $func Foldr t.func (e.value) e.list = e.value;
     66$public $func Foldr t.func (e.value) e.list = e.value;
    6867Foldr t.func (e.value) e.list =
    6968  (e.value) e.list $iter {
    70     e.list : e.init t.last =
    71       (<Apply t.func t.last e.value>) e.init;
    72   } :: (e.value) e.list,
    73   e.list : /*empty*/ =
     69    e.list : e.init t.last = (<Apply t.func t.last e.value>) e.init;
     70  } :: (e.value) e.list, e.list : /*empty*/ =
    7471  e.value;
    7572
    76 $func Foldr1 t.func v.list = e.value;
     73$public $func Foldr1 t.func v.list = e.value;
    7774Foldr1 t.func e.init t.last = <Foldr t.func (t.last) e.init>;
    7875
    79 All s.Fname e.Fargs (e.list) =
     76$public $func? All t.func e.list = ;
     77All t.func e.list =
    8078  e.list $iter {
    81     e.list : t.item e.rest =
    82       <Apply s.Fname e.Fargs t.item> : e,
    83       e.rest;
    84   } :: e.list,
    85   e.list : /*empty*/;
     79    e.list : t.head e.tail = <Apply t.func t.head> :: e, e.tail;
     80  } :: e.list, e.list : /*empty*/;
    8681
    87 Any s.Fname e.Fargs (e.list) = # \{
     82$public $func? Any t.func e.list = ;
     83Any t.func e.list = # \{
    8884  e.list $iter {
    89     e.list : t.item e.rest =
    90       # \{ <Apply s.Fname e.Fargs t.item> : e; },
    91       e.rest;
    92   } :: e.list,
    93   e.list : /*empty*/;
     85    e.list : t.head e.tail = # \{ <Apply t.func t.head > :: e; }, e.tail;
     86  } :: e.list, e.list : /*empty*/;
    9487};
    9588
     
    9891//<Const e.list> ~~~ <Apply &Concat e.list>;
    9992
    100 Paren expr = <Map &Paren_Term (expr)>;
     93//Paren expr = <Map &Paren_Term (expr)>;
     94//
     95//Reverse {
     96//  term e.list = <Reverse e.list> term;
     97//  /*empty*/ = /*empty*/;
     98//};
    10199
    102 Reverse {
    103   term e.list = <Reverse e.list> term;
    104   /*empty*/ = /*empty*/;
     100$public $func Intersperse (e.sep) e.list = e.list;
     101Intersperse {
     102  (e.sep) /*empty*/ = /*empty*/;
     103  (e.sep) t.head0 = t.head0;
     104  (e.sep) t.head0 e.tail0 =
     105    (t.head0 e.sep) e.tail0 $iter {
     106      e.list : t.head e.tail = (e.new_list t.head e.sep) e.tail;
     107    } :: (e.new_list) e.list, e.list : t.last =
     108    e.new_list t.last;
    105109};
    106110
    107 Intersperse {
    108   (e.sep) /*empty*/ = /*empty*/;
    109   (e.sep) t1 = t1;
    110   (e.sep) t1 e.list =
    111     (t1 e.sep) e.list $iter {
    112       e.list : t2 e.rest =
    113         (e.new_list t2 e.sep) e.rest;
    114     } :: (e.new_list) e.list,
    115     e.list : t2 =
    116     e.new_list t2;
    117 };
    118 
     111$public $func Separate (e.sep) e.list = e.listOfLists;
    119112Separate (e.sep) e.list =
    120   e.list (/*e.ll*/) $iter {
    121     e.list : e1 e.sep e2 = e2 (e.ll (e1));
     113  () e.list $iter {
     114    e.list : e.before e.sep e.after = (e.ll (e.before)) e.after;
    122115    (e.ll (e.list));
    123   } :: e.list (e.ll),
    124   e.list : /*empty*/ =
     116  } :: (e.ll) e.list, e.list : /*empty*/ =
    125117  e.ll;
    126118
    127 Nub e.expr = <NubBy &EqTerms e.expr>;
    128 
    129 NubBy s.eqTerms e.expr =
    130   e.expr () $iter {
    131     e.expr : term e.rest, {
    132       <Any s.eqTerms term (e.selected)> = e.rest (e.selected);
    133       e.rest (e.selected (term));
    134     };
    135   } :: e.expr (e.selected),
    136   e.expr : /*empty*/ =
    137   <Concat e.selected>;
    138 
    139 Replicate s.n e.expr =
    140   s.n /*e.list*/ $iter
    141     <Arithm.Sub s.n 1> e.list e.expr
    142   :: s.n e.list,
    143   <Le (s.n) (0)> =
    144   e.list;
    145 
    146 
    147 
    148 $func QSort_Split t.num e.list = (e.list) (e.list);
    149 
    150 QSort_Split t.num e.list,
    151   () () (e.list) $iter {
    152     e.list : t.head e.tail, {
    153       <Le (t.head) (t.num)>,
    154         (e.smaller t.head) (e.greater) (e.tail);
    155       (e.smaller) (e.greater t.head) (e.tail);
    156     };
    157   } :: (e.smaller) (e.greater) (e.list),
    158   e.list : /*empty*/,
    159   (e.smaller) (e.greater);
    160 
    161 QSort {
    162   /* empty */   = /* empty */;
    163   t.head e.tail = <QSort_Split t.head e.tail> :: (e.smaller) (e.greater),
    164     <QSort e.smaller> t.head <QSort e.greater>;
    165 };
    166 
    167 SortBy s.cmpTerms e.list =
    168   <Length e.list> :: s.len,
    169   {
    170     <Le (s.len) (1)> = e.list;
    171     <Div s.len 2> :: s.k =
    172       <Merge s.cmpTerms (<SortBy s.cmpTerms <Left 0 s.k e.list>>) (<SortBy s.cmpTerms <Middle s.k 0 e.list>>)>;
    173   };
    174 
    175 $func Merge s.cmpTerms (e.left) (e.right) = e.merged_list;
    176 Merge s.cmpTerms (e.left) (e.right) =
    177   (e.left) (e.right) $iter {
    178     e.left : t.l e.left_rest, e.right : t.r e.right_rest = {
    179       <Apply s.cmpTerms t.l t.r> : '<' = (e.left_rest) (e.right) e.merged t.l;
    180       = (e.left) (e.right_rest) e.merged t.r;
    181     };
    182   } :: (e.left) (e.right) e.merged,
    183   \{
    184     e.left  : /*empty*/ = e.merged e.right;
    185     e.right : /*empty*/ = e.merged e.left;
    186   };
    187 
    188 Sort e.list = <SortBy &CompareTerms e.list>;
    189 
    190 /*
    191  * Returns number of e.expr entries in e.source and e.source without all e.expr's.
    192  */
    193 //Entries e.expr (e.source) =
    194 //  0 e.source () $iter {
    195 //    e.source : e1 e.expr e2 = <"+" s.num 1> e2 (e.res e1);
    196 //    s.num (e.res e.source);
    197 //  } :: s.num e.source (e.res),
    198 //  e.source : /*empty*/ =
    199 //  s.num e.res;
    200 
    201 /*
    202  * Add to the first list all new elements from the second one.
    203  */
    204 Or (e.list1) e.list2 =
    205   (e.list1) e.list2 $iter {
    206     e.list2 : term e.rest,
    207       {
    208         e.list1 : e term e = (e.list1) e.rest;
    209         (e.list1 term) e.rest;
    210       };
    211   } :: (e.list1) e.list2,
    212   e.list2 : /*empty*/ =
    213   e.list1;
    214 
    215 /*
    216  * Choose from the first list all elements present in the second one.
    217  */
    218 And (e.list1) e.list2 =
    219   (e.list1) /*empty*/ $iter {
    220     e.list1 : term e.rest,
    221       {
    222         e.list2 : e term e = (e.rest) e.and term;
    223         (e.rest) e.and;
    224       };
    225   } :: (e.list1) e.and,
    226   e.list1 : /*empty*/ =
    227   e.and;
    228 
    229 /*
    230  * Choose from the first list all elements not present in the second one.
    231  */
    232 Sub (e.list1) e.list2 =
    233   (e.list1) /*empty*/ $iter {
    234     e.list1 : term e.rest,
    235       {
    236         e.list2 : e term e = (e.rest) e.not;
    237         (e.rest) e.not term;
    238       };
    239   } :: (e.list1) e.not,
    240   e.list1 : /*empty*/ =
    241   e.not;
    242 
     119//Nub e.expr = <NubBy &EqTerms e.expr>;
     120//
     121//NubBy s.eqTerms e.expr =
     122//  e.expr () $iter {
     123//    e.expr : term e.rest, {
     124//      <Any s.eqTerms term (e.selected)> = e.rest (e.selected);
     125//      e.rest (e.selected (term));
     126//    };
     127//  } :: e.expr (e.selected),
     128//  e.expr : /*empty*/ =
     129//  <Concat e.selected>;
     130//
     131//Replicate s.n e.expr =
     132//  s.n /*e.list*/ $iter
     133//    <Arithm.Sub s.n 1> e.list e.expr
     134//  :: s.n e.list,
     135//  <Le (s.n) (0)> =
     136//  e.list;
     137//
     138//
     139//
     140//$func QSort_Split t.num e.list = (e.list) (e.list);
     141//
     142//QSort_Split t.num e.list,
     143//  () () (e.list) $iter {
     144//    e.list : t.head e.tail, {
     145//      <Le (t.head) (t.num)>,
     146//        (e.smaller t.head) (e.greater) (e.tail);
     147//      (e.smaller) (e.greater t.head) (e.tail);
     148//    };
     149//  } :: (e.smaller) (e.greater) (e.list),
     150//  e.list : /*empty*/,
     151//  (e.smaller) (e.greater);
     152//
     153//QSort {
     154//  /* empty */   = /* empty */;
     155//  t.head e.tail = <QSort_Split t.head e.tail> :: (e.smaller) (e.greater),
     156//    <QSort e.smaller> t.head <QSort e.greater>;
     157//};
     158//
     159//SortBy s.cmpTerms e.list =
     160//  <Length e.list> :: s.len,
     161//  {
     162//    <Le (s.len) (1)> = e.list;
     163//    <Div s.len 2> :: s.k =
     164//      <Merge s.cmpTerms (<SortBy s.cmpTerms <Left 0 s.k e.list>>) (<SortBy s.cmpTerms <Middle s.k 0 e.list>>)>;
     165//  };
     166//
     167//$func Merge s.cmpTerms (e.left) (e.right) = e.merged_list;
     168//Merge s.cmpTerms (e.left) (e.right) =
     169//  (e.left) (e.right) $iter {
     170//    e.left : t.l e.left_rest, e.right : t.r e.right_rest = {
     171//      <Apply s.cmpTerms t.l t.r> : '<' = (e.left_rest) (e.right) e.merged t.l;
     172//      = (e.left) (e.right_rest) e.merged t.r;
     173//    };
     174//  } :: (e.left) (e.right) e.merged,
     175//  \{
     176//    e.left  : /*empty*/ = e.merged e.right;
     177//    e.right : /*empty*/ = e.merged e.left;
     178//  };
     179//
     180//Sort e.list = <SortBy &CompareTerms e.list>;
Note: See TracChangeset for help on using the changeset viewer.