Re: how to make this in prolog

From: Alan Bal jeu (jeu_at_sympatico.deleteme.ca)
Date: 03/26/04

  • Next message: zeus: "Re: Sublists question"
    Date: Thu, 25 Mar 2004 21:53:55 -0500
    
    

    "raf_go" <raf_go@go2.pl> wrote in message
    news:c3v3m9$dl0$1@atlantis.news.tpi.pl...
    > I nead make somthink like that in prolog
    >
    > x=1;
    > y=2;
    >
    > for(...){
    > a= 2^x * (2^y - 1);
    > cout<<a;
    > x+=1;
    > y+=1;
    > }
    >
    > for help thanks.

    Prolog does not have loops, but it does have recursion.
    Prolog has assignment (as a special case of unification),
    but not reassignment. This isn't a problem, because of recursion.
    Variables are capitals, sequencing is done using a comma,
    arithmetic using 'is'.

    So:
    weird_predicate :-
        X = 1,
        Y = 2,
        weird_predicate(X, Y).

    weird_predicate(X, Y) :-
        ..., %% you didn't specify how to stop, but you can do that here.
        A is 2 ^ X * (2^Y - 1),
        write(A),
        X1 is X + 1,
        Y1 is Y + 1,
        weird_predicate(X1, Y1).


  • Next message: zeus: "Re: Sublists question"

    Relevant Pages

    • Re: advice from a newbie
      ... previous opinion that Prolog's reliance on recursion ... Here's the usual Prolog implementation of reverse/2 ... Do there exist "closed" backtrack points as well then? ... Deterministic is a word used to describe predicates ...
      (comp.lang.prolog)
    • Re: pls confirm what approach is more efficient
      ... > recursion is a good thing because it can be transformed into iteration, ... > therefore stack usage is constant. ... > something more efficient that doesn't consume resources linear to the ... > Prolog, how can the most resource efficient implementation be picked ...
      (comp.lang.prolog)
    • Re: Minsky still posting
      ... >> claims to have found Prolog and recursion incredibly difficult to ... > mainly a hobbiest. ... He had problems understanding just about the simplest bit of Prolog ... I've spent my academic career in logic programming, ...
      (comp.lang.prolog)
    • Re: advice from a newbie
      ... I am extremely proud of the combination of using an aggregate ... seem to mistrust recursion and feel that Prolog ... and I would really like a version of member/2 which does not ... Probably Prolog programmers don't even know this, ...
      (comp.lang.prolog)
    • Re: Newbie, The secret with Prolog: How to to transform old "traditionally programming" t
      ... > programming before using Prolog. ... It is a lot like Scheme in that it actively encourages _intensive_ use ... Recursion doesn't remove that; it changes the shape of it. ... > in programming are hard for people. ...
      (comp.lang.prolog)