File: morse_enum.dats

package info (click to toggle)
ats2-lang 0.1.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,136 kB
  • ctags: 20,441
  • sloc: ansic: 354,696; makefile: 2,679; sh: 638
file content (83 lines) | stat: -rw-r--r-- 1,594 bytes parent folder | download | duplicates (2)
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
(*
//
// This code is based on some code by
// Eli Frey (eli.frey AT gmail DOT com)
//
// Minor modification by Hongwei Xi (gmhwxi AT gmail DOT com)
//
// Time: July 18-19, 2012
//
*)

(* ****** ****** *)
//
#include
"share/atspre_staload.hats"
//
(* ****** ****** *)
//
staload "libats/ML/SATS/string.sats"
//
staload _ = "libats/ML/DATS/string.dats"
//
(* ****** ****** *)

#define nil stream_nil
#define cons stream_cons
#define :: stream_cons

(* ****** ****** *)

fun
from{n:int} (
  n: intGte(n)
) :<!ntm> stream (intGte(n)) = $delay(n :: from{n+1}(n+1))

(* ****** ****** *)

typedef sstring = stream string

(* ****** ****** *)

fun morse
  (n: Nat):<!laz> sstring = let
//
  fn add_dot  (str: string):<> string = "." + str
  fn add_dash (str: string):<> string = "-" + str
//
  fn go (n: Nat):<> sstring = $effmask_all
  (
    case+ n of
    | 0 => $delay (""  :: $delay (nil))
    | 1 => $delay ("." :: $delay (nil))
    | n =>> let
        val add_dots   = stream_map_fun( morse( n-1 ), add_dot )
        val add_dashes = stream_map_fun( morse( n-2 ), add_dash )
      in
        stream_merge_fun( add_dots, add_dashes, lam (_, _) => 0 )
      end // end of [n]
  ) // end of [go]
in
  stream_nth_exn (stream_map_fun(from{0}(0), go), n)
end // end of [morse]

(* ****** ****** *)

staload STDIO = "libc/SATS/stdio.sats"

(* ****** ****** *)

implement
main0 () =
{
//
val xs = morse (5)
val xs = stream2list (xs)
val () = list_vt_foreach_fun (xs, lam (x) =<1> $STDIO.puts_exn (x))
val () = list_vt_free (xs)
//
} // end of [main]

(* ****** ****** *)

(* end of [morse_enum.dats] *)