packages feed

funcons-simple-0.1.0.3: tests/Kdiverse/Kdiverse3.fct

initialise-binding initialise-storing initialise-giving finalise-failing finalise-throwing scope
  (collateral (bind
    ("collatz",
     allocate-variable (functions
       (tuples ((values)*),
        values))),
  bind
    ("main",
     allocate-variable (functions
       (tuples ((values)*),
        values)))),
   sequential (assign
     (bound ("collatz"),
      function closure (scope
        (match
          (given,
           tuple (pattern closure (bind
             ("n",
              allocate-initialised-variable
                (values,
                 given))))),
         handle-return (scope
           (bind
             ("s",
              allocate-initialised-variable
                (values,
                 decimal-natural ("0"))),
            sequential
              (print ("Testing Collatz' conjecture for n = ",
              assigned (bound ("n")),
              " ... "),
               while
                 (is-greater
                   (assigned (bound ("n")),
                    decimal-natural ("1")),
                  sequential
                    (effect (give
                      (integer-add
                        (assigned (bound ("s")),
                         decimal-natural ("1")),
                       sequential
                         (assign
                           (bound ("s"),
                            given),
                          given))),
                     if-else
                       (is-equal
                         (assigned (bound ("n")),
                          integer-multiply
                            (checked integer-divide
                              (assigned (bound ("n")),
                               decimal-natural ("2")),
                             decimal-natural ("2"))),
                        effect (give
                          (checked integer-divide
                            (assigned (bound ("n")),
                             decimal-natural ("2")),
                           sequential
                             (assign
                               (bound ("n"),
                                given),
                              given))),
                        effect (give
                          (integer-add
                            (integer-multiply
                              (decimal-natural ("3"),
                               assigned (bound ("n"))),
                             decimal-natural ("1")),
                           sequential
                             (assign
                               (bound ("n"),
                                given),
                              given)))))),
               print ("Done! It took ",
               assigned (bound ("s")),
               " steps.\n"))))))),
   assign
     (bound ("main"),
      function closure (scope
        (match
          (given,
           tuple ()),
         handle-return (sequential
           (print ("Testing Collatz' conjecture up to what number? "),
            scope
              (bind
                ("m",
                 allocate-initialised-variable
                   (values,
                    read)),
               sequential
                 (scope
                   (bind
                     ("i",
                      allocate-initialised-variable
                        (values,
                         decimal-natural ("1"))),
                    while
                      (is-less-or-equal
                        (assigned (bound ("i")),
                         assigned (bound ("m"))),
                       sequential
                         (effect (apply
                           (assigned (bound ("collatz")),
                            tuple (assigned (bound ("i"))))),
                          effect (give
                            (bound ("i"),
                             sequential
                               (assign
                                 (given,
                                  integer-add
                                    (assigned (given),
                                     1)),
                                assigned (given))))))),
                  print ("Done.  It appears to hold.\n")))))))),
   apply
     (assigned (bound ("main")),
      tuple ())))