Jump to content

Kernighans large earthquake problem: Difference between revisions

Added Prolog Solution
m (→‎AppleScript :: Functional: Added `either` to return any Left message as a string.)
(Added Prolog Solution)
Line 869:
{{Out}}
<pre style="font-size:84%">
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
Example command line: <code>swipl kernighans_earthquake.pl earthquake.txt</code>.
<lang prolog>:- initialization(main, main).
 
process_line(Line):-
split_string(Line, "\s\t", "\s\t", [_, _, Magnitude_string]),
read_term_from_atom(Magnitude_string, Magnitude, []),
Magnitude > 6,
!,
writef('%w\n', [Line]).
process_line(_).
 
process_stream(Stream):-
read_line_to_string(Stream, String),
String \= end_of_file,
!,
process_line(String),
process_stream(Stream).
process_stream(_).
 
process_file(File):-
open(File, read, Stream),
process_stream(Stream),
close(Stream).
 
main([File]):-
process_file(File),
!.
main(_):-
swritef(Message, 'File argument is missing\n', []),
write(user_error, Message).</lang>
 
{{out}}
<pre>
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.