challenge 条件を満たす行を取り除く

ファイルから1行ずつ読み込み、"#"で始まる行だけを取り除いてファイルに出力するコードを書いてください。

サンプル入力

hello!
# remove this
 # don't remove this
bye!
サンプル出力
hello!
 # don't remove this
bye!

Posted feedbacks - Prolog

とりあえず、ライブラリを使わずに。
1
2
3
4
5
6
7
8
lineLoop:-peek_char(end_of_file).
lineLoop:- line(B),(B=['#'|_]->write('');string_to_list(Str,B),write(Str)),lineLoop.

line([]):-peek_char(end_of_file).
line(['\n']):-peek_char('\n'),get_char(_).
line([C|Cs]):-get_char(C),line(Cs).

:- prompt(_,''),lineLoop, halt.

read_line_to_codeを使うと、ユーザー入力にした場合、入力を待ってくれないのをどうすればよいのか判らないのです。
1
2
3
4
5
6
lineLoop(Str):-
        read_line_to_codes(Str,Line),
        (Line=[35|_]->write("");
         (not(Line=end_of_file)->string_to_list(S,Line),writeln(S),lineLoop(Str); true)).

:- prompt(_,''),current_stream(_,'read',Str),lineLoop(Str), halt.

Index

Feed

Other

Link

Pathtraq

loading...