Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!batcomputer!munnari.oz.au!bruce!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.prolog Subject: Re: simple question Message-ID: <5021@goanna.cs.rmit.oz.au> Date: 21 Mar 91 05:03:35 GMT References: <1991Mar20.225013.21372@comp.vuw.ac.nz> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 103 In article <1991Mar20.225013.21372@comp.vuw.ac.nz>, Bill.Viggers@comp.vuw.ac.nz (Bill Viggers) writes: > For starters its easiest if you give Prolog functions, names that > describe what they do. Personally I also dislike > have two functions with the same name and different arity. Prolog has no functions, only predicates. I very much *like* using the same symbol at different arities, as it reduces the need for inventing names whose only raison d'etre is to be different. There are some improvements that can be made to Viggers' version. > writeln(String):- > write(String), > nl. Why "String"? The argument can be *any* term, and in the actual program it isn't any kind of string, it's an atom. Better: writeln(Message) :- write(Message), nl. > makelist:- > writeln('Enter the verticies'), > getlist(List), > write(List). When an identifier is made out of several words, it is a good idea to make it clear to the reader where the word divisions are (contrast lighthouse_keeper light_housekeeper). It is also a good idea to spell everything right, including comments, and most especially text which is displayed to users. 'vertexes' would do at a pinch, but it's 'verticies'. It is far from clear to me why writeln/1 is used here. It would be nice to use a message like this as a prompt. The predicate doesn't make a list. It reads it in and then writes it. What's more, "list" is rather too vague to be useful. The ISO crowd even used "list" with the specific meaning "list of characters". If this is a list of vertices, call it Vertices. read_then_write_vertices :- writeln('Please enter the vertices.'), read_vertices(Vertices), writeln(Vertices). > getlist(Thelist):- > read(Item), > addtolist(Item,_,Thelist). > > addtolist(end,_,end):-!. > > addtolist(Item,List,Thelist):- > Thelist=[Item|List], > writeln('Another one...'), > getlist(List). This *HAS* to be a joke. Why end a list with 'end'? Why not end it with [], which is what lists usually end with? read_vertices(Vertices) :- write('Next vertex: '), read(Vertex), read_vertices(Vertex, Vertices). read_vertices(end, Vertices) :- !, Vertices = []. read_vertices(Vertex, [Vertex|Vertices]) :- read_vertices(Vertices). % sicstus SICStus 0.7 #1: Tue Sep 4 21:39:16 EST 1990 | ?- [foo]. {consulting /r/staff/ok/foo...} {/r/staff/ok/foo consulted, 116 msec 1480 bytes} yes | ?- read_then_write_vertices. Please enter the vertices. Next vertex: tom. Next vertex: tom. Next vertex: the. Next vertex: pipers. Next vertex: son. Next vertex: end. [tom,tom,the,pipers,son] yes One more point: it might be even better to use 'end_of_file' as the end marker, then the user can just use his to end the input, instead of having to know what the special magic marker is. If you _don't_ do that, then you really should remind the user what to type when s/he wants to stop: read_then_write_vertices :- writeln('Please enter the vertices. Use 'end' to stop.'), read_vertices(Vertices), writeln(Vertices). -- Seen from an MVS perspective, UNIX and MS-DOS are hard to tell apart.