🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #4124

Re: A Challenge

#4124
From: Mark William Hopkins
Date: Sun, 24 Dec 1989 03:05
37 lines
1718 bytes
In article <11500022@hpldoda.UUCP> patch@hpldoda.UUCP (Pat Chkoreff) writes:
>Here's a challenge for you.  It's a stripped-down version of a problem I've
>been working on, and I haven't solved it yet.
>
>Write a predicate good/1 which succeeds if and only if its argument is a
>list of lists, where all of the lists have the same length.

This one I think is worth posting, because of the style in addition to its
being a solution.  It makes full use of the user-defineable precedence
grammar, as all good Prolog programs should, with the effect of giving it
the feel of natural language.

:- op(50, xf, good).
:- op(50, xfx, are_length).
:- op(50, xfx, is_length).

[] good :- !.
[Xs | Xss] good :- Xs is_length L, Xss are_length L.

[] are_length _ :- !.
[Xs | Xss] are_length L :- Xs is_length L, Xss are_length L.

[] is_length 0 :- !.
[_ | Xs] is_length L1 :- Xs is_length L, L1 is L + 1.

>The predicate should be written using pure Horn clauses -- no calls to !/0
>or var/1.

You can replace the clauses with cuts in them by unit-clauses.

>'Same length' is defined by:
>
>    same_length([], []).
>    same_length([_|Xs], [_|Ys]) :-
>        same_length(Xs, Ys).

same_length(Xs, Ys) :- Xs is_length L, Ys is_length L.
Technical Details
Message-ID: <1633@uwm.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!wuarchive!uwm.edu!csd4.csd.uwm.edu!markh
References: <11500022@hpldoda.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!wuarchive!uwm.edu!csd4.csd.uwm.edu!markh
From: markh@csd4.csd.uwm.edu (Mark William Hopkins)
Newsgroups: comp.lang.prolog
Subject: Re: A Challenge
Message-ID: <1633@uwm.edu>
Date: 24 Dec 89 03:05:35 GMT
References: <11500022@hpldoda.UUCP>
Sender: news@uwm.edu
Reply-To: markh@csd4.csd.uwm.edu (Mark William Hopkins)
Organization: University of Wisconsin-Milwaukee
Lines: 37