Newsgroups: comp.lang.ada,comp.lang.apl,comp.lang.basic.misc,comp.lang.basic.visual.misc,comp.lang.c,comp.lang.c++,comp.lang.clos,comp.lang.eiffel,comp.lang.forth,comp.lang.fortran,comp.lang.lisp,comp.lang.misc,comp.lang.modula2,comp.lang.oberson,comp.lang.pascal,comp.lang.prolog,comp.lang.smalltalk,comp.lang.objective-c,comp.lang.functional
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!oitnews.harvard.edu!purdue!lerc.nasa.gov!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!tank.news.pipex.net!pipex!in1.uu.net!sparky!kwiudl.kwi.com!usenet
From: tom@kwi.com (Tom Howland)
Subject: Re: Please help with research
In-Reply-To: jimw@math.umass.edu's message of 15 Aug 1995 14:29:29 GMT
Message-ID: <TOM.95Aug18151354@heather.kwi.com>
Lines: 159
Sender: usenet@kwiudl.kwi.com
Organization: KnowledgeWare Inc, Redwood City, CA 94065-1417
References: <19950814T190825Z@naggum.no> <40qb09$km7@nic.umass.edu>
Date: Fri, 18 Aug 1995 22:13:54 GMT
Xref: glinda.oz.cs.cmu.edu comp.lang.ada:34088 comp.lang.apl:7224 comp.lang.basic.misc:8260 comp.lang.basic.visual.misc:31719 comp.lang.c:151979 comp.lang.c++:144730 comp.lang.clos:3409 comp.lang.eiffel:10464 comp.lang.forth:23601 comp.lang.fortran:31363 comp.lang.lisp:18842 comp.lang.misc:22757 comp.lang.modula2:12390 comp.lang.prolog:13690 comp.lang.smalltalk:27386 comp.lang.objective-c:4383 comp.lang.functional:6221

Here's one written in Prolog

% hex dump ... a prolog version of the famous hex dump challenge,
% written in Quintus Prolog.

:- use_module(library(ctypes), [is_print/1]).

bytes_per_line(16).

runtime_entry(start) :- unix(argv([FileName])), go(FileName).

go(FileName) :- see(FileName), dump(0), seen.

dump(Count) :- in(1, L, Done), out(Count, L),
    (   Done == true
    ->  true
    ;   N is Count+1, dump(N)
    ).

in(N, L, Done) :-	% read in a line
    (   bytes_per_line(N)
    ->  L = []
    ;   get0(C),
        (   C =:= -1	% -1 indicates end of file
	->  Done = true, L = []
	;   L = [C|T], M is N+1, in(M, T, Done)
	)
    ).

out(Count, L) :-	% print a line
    (   L == []
    ->  true
    ;   pad(3, Count), format('~16r ', [Count]), out(L), length(L, N),
        bytes_per_line(B), X is 2*(B-N)+1, format('~*c', [X,32]), outc(L), nl
    ).

pad(Pad, C) :-	% provide leading zeros so the columns come out neat
    N is Pad-1,
    (  N =:= 0
    -> true
    ;  X is C >> 4,
       (   X =:= 0
       ->  put(0'0)
       ;   true
       ),
       pad(N, X)
    ).

out([]).	% print the hex dump
out([H|T]) :-
    (   H < 16
    ->  put(0'0)
    ;   true
    ),
    format('~16r', [H]),
    out(T).

outc([]).	% print its ascii equivalent
outc([H|T]) :-
    (   is_print(H)
    ->  put(H)
    ;   put(0'.)
    ),
    outc(T).

------

invoked on itself gives:

------

000 25206865782064756d70202e2e2e20   % hex dump ... 
001 612070726f6c6f672076657273696f   a prolog versio
002 6e206f66207468652066616d6f7573   n of the famous
003 206865782064756d70206368616c6c    hex dump chall
004 656e67652c0a25207772697474656e   enge,.% written
005 20696e205175696e7475732050726f    in Quintus Pro
006 6c6f672e0a0a3a2d207573655f6d6f   log...:- use_mo
007 64756c65286c696272617279286374   dule(library(ct
008 79706573292c205b69735f7072696e   ypes), [is_prin
009 742f315d292e0a0a62797465735f70   t/1])...bytes_p
00a 65725f6c696e65283136292e0a0a72   er_line(16)...r
00b 756e74696d655f656e747279287374   untime_entry(st
00c 61727429203a2d20756e6978286172   art) :- unix(ar
00d 6776285b46696c654e616d655d2929   gv([FileName]))
00e 2c20676f2846696c654e616d65292e   , go(FileName).
00f 0a0a676f2846696c654e616d652920   ..go(FileName) 
010 3a2d207365652846696c654e616d65   :- see(FileName
011 292c2064756d702830292c20736565   ), dump(0), see
012 6e2e0a0a64756d7028436f756e7429   n...dump(Count)
013 203a2d20696e28312c204c2c20446f    :- in(1, L, Do
014 6e65292c206f757428436f756e742c   ne), out(Count,
015 204c292c0a2020202028202020446f    L),.    (   Do
016 6e65203d3d20747275650a20202020   ne == true.    
017 2d3e2020747275650a202020203b20   ->  true.    ; 
018 20204e20697320436f756e742b312c     N is Count+1,
019 2064756d70284e290a20202020292e    dump(N).    ).
01a 0a0a696e284e2c204c2c20446f6e65   ..in(N, L, Done
01b 29203a2d0925207265616420696e20   ) :-.% read in 
01c 61206c696e650a2020202028202020   a line.    (   
01d 62797465735f7065725f6c696e6528   bytes_per_line(
01e 4e290a202020202d3e20204c203d20   N).    ->  L = 
01f 5b5d0a202020203b20202067657430   [].    ;   get0
020 2843292c0a20202020202020202820   (C),.        ( 
021 202043203d3a3d202d310925202d31     C =:= -1.% -1
022 20696e6469636174657320656e6420    indicates end 
023 6f662066696c650a092d3e2020446f   of file..->  Do
024 6e65203d20747275652c204c203d20   ne = true, L = 
025 5b5d0a093b2020204c203d205b437c   []..;   L = [C|
026 545d2c204d206973204e2b312c2069   T], M is N+1, i
027 6e284d2c20542c20446f6e65290a09   n(M, T, Done)..
028 290a20202020292e0a0a6f75742843   ).    )...out(C
029 6f756e742c204c29203a2d09252070   ount, L) :-.% p
02a 72696e742061206c696e650a202020   rint a line.   
02b 20282020204c203d3d205b5d0a2020    (   L == [].  
02c 20202d3e2020747275650a20202020     ->  true.    
02d 3b20202070616428332c20436f756e   ;   pad(3, Coun
02e 74292c20666f726d617428277e3136   t), format('~16
02f 7220272c205b436f756e745d292c20   r ', [Count]), 
030 6f7574284c292c206c656e67746828   out(L), length(
031 4c2c204e292c0a2020202020202020   L, N),.        
032 62797465735f7065725f6c696e6528   bytes_per_line(
033 42292c205820697320322a28422d4e   B), X is 2*(B-N
034 292b312c20666f726d617428277e2a   )+1, format('~*
035 63272c205b582c33325d292c206f75   c', [X,32]), ou
036 7463284c292c206e6c0a2020202029   tc(L), nl.    )
037 2e0a0a706164285061642c20432920   ...pad(Pad, C) 
038 3a2d09252070726f76696465206c65   :-.% provide le
039 6164696e67207a65726f7320736f20   ading zeros so 
03a 74686520636f6c756d6e7320636f6d   the columns com
03b 65206f7574206e6561740a20202020   e out neat.    
03c 4e206973205061642d312c0a202020   N is Pad-1,.   
03d 202820204e203d3a3d20300a202020    (  N =:= 0.   
03e 202d3e20747275650a202020203b20    -> true.    ; 
03f 20582069732043203e3e20342c0a20    X is C >> 4,. 
040 2020202020202820202058203d3a3d         (   X =:=
041 20300a202020202020202d3e202070    0.       ->  p
042 757428302730290a20202020202020   ut(0'0).       
043 3b202020747275650a202020202020   ;   true.      
044 20292c0a2020202020202070616428    ),.       pad(
045 4e2c2058290a20202020292e0a0a6f   N, X).    )...o
046 7574285b5d292e0925207072696e74   ut([])..% print
047 20746865206865782064756d700a6f    the hex dump.o
048 7574285b487c545d29203a2d0a2020   ut([H|T]) :-.  
049 20202820202048203c2031360a2020     (   H < 16.  
04a 20202d3e202070757428302730290a     ->  put(0'0).
04b 202020203b202020747275650a2020       ;   true.  
04c 2020292c0a20202020666f726d6174     ),.    format
04d 28277e313672272c205b485d292c0a   ('~16r', [H]),.
04e 202020206f75742854292e0a0a6f75       out(T)...ou
04f 7463285b5d292e0925207072696e74   tc([])..% print
050 206974732061736369692065717569    its ascii equi
051 76616c656e740a6f757463285b487c   valent.outc([H|
052 545d29203a2d0a2020202028202020   T]) :-.    (   
053 69735f7072696e742848290a202020   is_print(H).   
054 202d3e20207075742848290a202020    ->  put(H).   
055 203b2020207075742830272e290a20    ;   put(0'.). 
056 202020292c0a202020206f75746328      ),.    outc(
057 54292e0a                         T)..
