tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
26
Task/Repeat-a-string/Mercury/repeat-a-string.mercury
Normal file
26
Task/Repeat-a-string/Mercury/repeat-a-string.mercury
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
:- module repeat.
|
||||
:- interface.
|
||||
:- import_module string, char, int.
|
||||
|
||||
:- func repeat_char(char, int) = string.
|
||||
:- func repeat(string, int) = string.
|
||||
|
||||
:- implementation.
|
||||
:- import_module stream, stream.string_writer, string.builder.
|
||||
|
||||
repeat_char(C, N) = string.duplicate_char(C, N).
|
||||
|
||||
repeat(String, Count) = Repeated :-
|
||||
S0 = string.builder.init,
|
||||
Repeated = string.builder.to_string(S),
|
||||
printn(string.builder.handle, Count, String, S0, S).
|
||||
|
||||
:- pred printn(Stream, int, string, State, State)
|
||||
<= (stream.writer(Stream, string, State),
|
||||
stream.writer(Stream, character, State)).
|
||||
:- mode printn(in, in, in, di, uo) is det.
|
||||
printn(Stream, N, String, !S) :-
|
||||
( N > 0 ->
|
||||
print(Stream, String, !S),
|
||||
printn(Stream, N - 1, String, !S)
|
||||
; true ).
|
||||
Loading…
Add table
Add a link
Reference in a new issue