File: test_rec_fun.ml

package info (click to toggle)
js-of-ocaml 5.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,020 kB
  • sloc: ml: 91,250; javascript: 57,289; ansic: 315; makefile: 271; lisp: 23; sh: 6; perl: 4
file content (111 lines) | stat: -rw-r--r-- 2,479 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
(**************************************************************************)
(*                                                                        *)
(*                                OCaml                                   *)
(*                                                                        *)
(*   Copyright 2019 Institut National de Recherche en Informatique et     *)
(*     en Automatique.                                                    *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

let rec h =
  let rec f n = if n >= 0 then g (n - 1)
  and g n =
    h n;
    f n
  in
  f

let () = ignore (h 10)

let mooo x =
  let rec h =
    ignore (Sys.opaque_identity x);
    let rec g n =
      h n;
      f n
    and f n = if n >= 0 then g (n - 1) in
    f
  in
  h

let h = mooo 3

let () = ignore (h 10)

let rec foo =
  let rec f = function
    | 0 -> 100
    | n -> foo (n - 1)
  and g = function
    | 0 -> 200
    | n -> f (n - 1)
  in
  g

let%expect_test _ =
  print_int (foo 2);
  print_newline ();
  [%expect {| 200 |}]

let%expect_test _ =
  print_int (foo 7);
  print_newline ();
  [%expect {| 100 |}]

let with_free_vars a b c =
  let rec foo =
    let rec f = function
      | 0 -> 100 + a + b + c
      | n -> foo (n - 1)
    and g = function
      | 0 -> 200 + a + b + c
      | n -> f (n - 1)
    in
    g
  in
  foo

let%expect_test _ =
  print_int (with_free_vars 1 2 3 2);
  print_newline ();
  [%expect {| 206 |}]

let%expect_test _ =
  print_int (with_free_vars 1 2 3 7);
  print_newline ();
  [%expect {| 106 |}]

let bar =
  let rec f = function
    | 0 -> 3
    | n -> g (n - 1)
  and g = function
    | 0 -> 10 + f 10
    | n -> f (n - 1)
  in
  let foof = f and goof = g in
  foof, goof

let%expect_test _ =
  print_int (snd bar 42);
  print_newline ();
  [%expect {| 13 |}]

let rec foobar =
  let rec f x = function
    | 0 -> 100
    | n -> foobar x (n - 1)
  and g x = function
    | 0 -> 200
    | n -> f x (n - 1)
  in
  g

let%expect_test _ =
  print_int (foobar 5 2);
  print_newline ();
  [%expect {| 200 |}]