Newsgroups: comp.lang.prolog
From: Bart Demoen <b...@cs.kuleuven.ac.be>
Date: Thu, 07 Feb 2008 13:56:38 +0100
Local: Thurs, Feb 7 2008 7:56 am
Subject: Re: This was memory fragmentation
Jan Wielemaker wrote: [and then some fac/2 definitions] I like these versions of fac/2 a lot - not because they are so Here is the first version in the SICStus manual: fac1(0, 1). Let's have a look at what the SICStus manual says is the preferred fac_pref(N, X) :- Note first the different semantics: ?- fac1(1-1,X). ?- fac1(2-1,X). X = 1 ; ?- fac_pref(1-1,X). X = 1 ?- fac_pref(2-1,X). X = 1 The fact that Prolog evaluates variables in arithmetic is very sneaky. In the "preferred" version, there is no good reason to have the Now Jan's first version: facjan1(N, X) :- ?- facjan1(1-1,X). X = 1 Clearly not what one wants, and the origin is the same. facjan_pref(N, I) :- fac2(0, 1) :- !. ?- facjan_pref(0,X). X = 1 So, facjan_pref is explicit: the N must be a number (and an int and not negative). I guess the cut after the N > 0 test in the last clause remained there Now, the code for facjan_pref/fac2 specifies twice that the N must be ?- facjan_pref(0,7). which is due to the first clause of fac2: it should have been fac2(0,Res) :- !, Res = 1. Given that experts (Jan, Vitor) make subtle mistakes, and given the wouldn't the following be a better way to write fac/2: :- type facbartpref(nonnegint:in, int:out). % (*) facbartpref(0,1). I have replaced the N > 0 by N \== 0. A decent compiler should be able Why is the Prolog community still so scared of type/mode declarations, Cheers Bart Demoen You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||