/* ABC problem in Picat v3. From http://rosettacode.org/wiki/ABC_Problem """ You are given a collection of ABC blocks. Just like the ones you had when you were a kid. There are twenty blocks with two letters on each block. You are guaranteed to have a complete alphabet amongst all sides of the blocks. The sample blocks are: ((B O) (X K) (D Q) (C P) (N A) (G T) (R E) (T G) (Q D) (F S) (J W) (H U) (V I) (A N) (O B) (E R) (F S) (L Y) (P C) (Z M)) The goal of this task is to write a function that takes a string and can determine whether you can spell the word with the given collection of blocks. The rules are simple: Once a letter on a block is used that block cannot be used again The function should be case-insensitive Show your output on this page for the following words: Example >>> can_make_word("A") True >>> can_make_word("BARK") True >>> can_make_word("BOOK") False >>> can_make_word("TREAT") True >>> can_make_word("COMMON") False >>> can_make_word("SQUAD") True >>> can_make_word("CONFUSE") True """ Cf http://hakank.org/picat/abc_problem.pi (Picat v2) This program is ported from the Prolog solution. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import v3_utils. main => go. go ?=> abc_problem, nl. go => true. abc_problem :- member(Word, ['', 'A', bark, bOOk, treAT, 'COmmon', sQuaD, 'CONFUSE',schoolmastering]), abc_problem(Word), fail, nl. abc_problem2 :- maplist(abc_problem, ['', 'A', bark, bOOk, treAT, 'COmmon', sQuaD, 'CONFUSE',schoolmastering]). abc_problem(Word) :- L = [[b,o],[x,k],[d,q],[c,p],[n,a],[g,t],[r,e],[t,g],[q,d],[f,s], [j,w],[h,u],[v,i],[a,n],[o,b],[e,r],[f,s],[l,y],[p,c],[z,m]], ( abc_problem(L, Word) -> bp.format('~w OK~n', [Word]) ; bp.format('~w KO~n', [Word])). abc_problem(L, Word) :- bp.atom_chars(Word, C_Words), maplist(to_lowercase, C_Words, D_Words), can_makeword(L, D_Words). to_lowercase(C,Lower) :- Lower = to_lowercase(C). can_makeword(_L, []). can_makeword(L, [H | T]) :- ( select([H, _], L, L1); select([_, H], L, L1)), can_makeword(L1, T).