Shen prolog factorial

108 views
Skip to first unread message

Chris Double

unread,
Sep 29, 2016, 5:39:00 AM9/29/16
to qil...@googlegroups.com
I tried a definition of factorial in Prolog using Shen:

(defprolog fac
0 1 <--;
N R <-- (when (> N 0))
(is N1 (- N 1))
(fac N1 R1)
(is R (* N R1));
)

The equivalent definition in Prolog:

fac(0,1).
fac(N,R) :- N > 0,
N1 is N - 1,
fac(N1, R1),
R is N * R1.

In prolog I can do:

?- fac(5,120).
true ;

But in Shen this doesn't work:

(prolog? (fac 5 120))
vector-ref: not a vector: (120)

But this does give a result of 120:

(prolog? (fac 5 X) (return X))
120

What am I doing wrong?

I'm using the Shen Scheme version on Chibi Scheme.
--
http://bluishcoder.co.nz

Mark Tarver

unread,
Sep 29, 2016, 5:57:07 AM9/29/16
to qil...@googlegroups.com
Because the is in Shen Prolog was specifically designed to bind a variable and its found a number and not a variable.   

(prolog? (fac 5 X) (identical X 120))  will work

Mark

--
http://bluishcoder.co.nz

--
You received this message because you are subscribed to the Google Groups "Shen" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qilang+unsubscribe@googlegroups.com.
To post to this group, send an email to qil...@googlegroups.com.
Visit this group at https://groups.google.com/group/qilang.
For more options, visit https://groups.google.com/d/optout.

Chris Double

unread,
Sep 29, 2016, 5:03:49 PM9/29/16
to qil...@googlegroups.com
On Thu, Sep 29, 2016 at 10:57 PM, Mark Tarver <dr.mt...@gmail.com> wrote:
> Because the is in Shen Prolog was specifically designed to bind a variable
> and its found a number and not a variable.
>
> (prolog? (fac 5 X) (identical X 120)) will work

Thanks for that. I like to write posts about my progress learning
languages and I've written a post documenting my exploration with Shen
Prolog:

https://bluishcoder.co.nz/2016/08/30/kicking-the-tires-of-shen-prolog.html

Hopefully I haven't got too much wrong!

--
http://bluishcoder.co.nz

Mark Tarver

unread,
Sep 30, 2016, 3:51:37 AM9/30/16
to qil...@googlegroups.com
It's not actually hard to make Shen is consistent with Prolog is;  I'll look at this.

Mark

Mark Tarver

unread,
Oct 1, 2016, 7:23:02 AM10/1/16
to Shen
Jolly good Chris.  An informative article.

Mark
Reply all
Reply to author
Forward
0 new messages