🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #1049

Re: Simple Prolog Question

#1049
From: R. Kym Horsell
Date: Wed, 08 Aug 1990 17:29
35 lines
1652 bytes
In article <2157@cernvax.UUCP> julian@cernvax.UUCP (julian bunn) writes:
>Can some kind person please help with a trivial Prolog problem?
>I am trying to write a program that will remove leading blank
>characters from a list. For example, the result of:
[stuff deleted]
>remove_blanks([H|T],T) :- H=32, remove_blanks(T,_).
>remove_blanks(S,S).
>
>which, when tracing, seems to do nicely recursively, but in the
>end returns me with T equal to the original list!


There are a couple of problems with your code.

The first is that clause 1 doesn't look past the first blank
in the string. You want:

remove_blanks([H|T],T1) :- H=32, remove_blanks(T,T1).

Also, to prevent backtracking to the second clause if
something *subsequent* to calling remove_blanks/2 fails,
you don't want the second clause to succeed if the first
did. One way to do this is with a cut. Another method is to 
use a test thus:

remove_blanks(S,S) :- not(S=[32|_]).

You can also use more unification in the first clause to give a final 
version something like:

remove_blanks([32|T],T1) :- !, remove_blanks(T,T1).
remove_blanks(S,S) :- not(S=[32|_]).

-Kym
===
Technical Details
Message-ID: <3792@bingvaxu.cc.binghamton.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!leah!bingvaxu!vu0310
References: <2157@cernvax.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!leah!bingvaxu!vu0310
From: vu0310@bingvaxu.cc.binghamton.edu (R. Kym Horsell)
Newsgroups: comp.lang.prolog
Subject: Re: Simple Prolog Question
Message-ID: <3792@bingvaxu.cc.binghamton.edu>
Date: 8 Aug 90 17:29:26 GMT
References: <2157@cernvax.UUCP>
Reply-To: vu0310@bingvaxu.cc.binghamton.edu.cc.binghamton.edu (R. Kym Horsell)
Organization: SUNY Binghamton, NY
Lines: 35