0

My base definition

use_module(library(clpfd)).
/* base definitions */
ice_cream(vanilla).
ice_cream(chocolate).
ice_cream(strawberry).

Turn words into integers

/*get falavor position */
icecream_index(Icecream,Icecreamlist,Start,End) :-
    var(Icecream),
    Icecream in Start .. End.
icecream_index(Icecream,Icecreamlist,Start,End) :-
    \+ var(Icecream),
    nth0(StartEnd,Icecreamlist,Icecream),
    Icecream is StartEnd,
    Icecream in StartEnd .. StartEnd.

Main program that generates all combinations

/* generate all combinations of ice cream */
icecream_menu(MenuList) :-
    MenuList = [Toppingone, Toppingtwo, Toppingthree],
    findall(X,ice_cream(X),Icecreamlist),

    length(Icecreamlist, Icecreamcount),

    Icecreamstart is 1,
    Icecreamend is Icecreamcount,

    icecream_index(Toppingone,Icecreamlist,Icecreamstart,Icecreamend),
    icecream_index(Toppingtwo,Icecreamlist,Icecreamstart,Icecreamend),
    icecream_index(Toppingthree,Icecreamlist,Icecreamstart,Icecreamend),

    all_different(MenuList),
    label(MenuList).

When I call MenuList(X), giving empty variable it is able to generate all possible combinations, however when I give it a list, MenuList([vanilla, X, Y]) it just fails when it should generate all combinations of ice cream topping starting with vanilla.

A second problem I can't figure out it how to turn the integer to words, so instead of getting Menu = [1, 2, 3] I want to get Menu = [vanilla, chocolate, strawberry].

false
  • 10,264
  • 13
  • 101
  • 209

0 Answers0