Prolog arithmetic syntax

前端 未结 3 1597
清酒与你
清酒与你 2021-01-14 01:32

How to define a as a integer/float number ?

I want to find the results of a+b+c+d=10 where a,b,c,d is integer and >=0

3条回答
  •  遥遥无期
    2021-01-14 02:29

    Here is GNU-Prolog piece of code with constraint solving over finite domains :

    $ gprolog
    | ?- [user].
    compiling user for byte code...
    ten(A,B,C,D) :- fd_domain([A,B,C,D],0,9999999), 10 #= A + B + C + D.
    

    Ctrl + D

    | ?- ten(A,B,C,D), fd_labeling([A,B,C,D]).
    

    As you can see, it solves problem of big ranges like 0-9999999

    A = 0
    B = 0
    C = 0
    D = 10 ? ;
    
    A = 0
    B = 0
    C = 1
    D = 9 ? ;
    
    A = 0
    B = 0
    C = 2
    D = 8 ? ;
    ...
    

    P.S. Thanks for Przemysław Kobylański for his blog with clear, very nice Prolog examples, where I've found inspiring examples.

    P.P.S. When playing with finite domains, you might like to use fd_set_vector_max/1 . In above case it's not needed, but depending on constraint might be usefull - more details when Gnu-Prolog operates on ranges, when on vectors of possible values, can be found at manual "Finite domain solver and built-in predicates - Introduction"

提交回复
热议问题