Skip to content

Instantly share code, notes, and snippets.

@gbmele
Created March 18, 2024 11:17
Show Gist options
  • Save gbmele/c85ece7f10e8b6b7d04c9d402b9e7b59 to your computer and use it in GitHub Desktop.
Save gbmele/c85ece7f10e8b6b7d04c9d402b9e7b59 to your computer and use it in GitHub Desktop.
extensible records in prolog
attr(A, [N-V | R]):- memberchk( N-X, A), X = V, attr(A, R).
attr(_, []).
color(A, B):- attr( A, [color-B]).
pigs( Pigs):-
length( Pigs,N),
N rem 2 =:= 1, Middle is N div 2, /* there _is_ a middle - list length is odd */
nth0( Middle,Pigs,P1), attr( P1, [color-brown]),
member( P2, Pigs), attr( P2, [color-brown, eats-carrots]),
member( P3, Pigs), attr( P3, [eats-salad, sound-giggles]),
right( P4,P4b,Pigs), attr( P4, [eats-salad]), attr( P4b, [color-brown]),
left( P5,P5b,Pigs), attr( P5, [color-black]), attr( P5b, [sound-squeaks]),
member( P6, Pigs), attr( P6, [color-black, sound-grumbles]),
member( P7, Pigs), attr( P7, [color-grey, sound-giggles]),
member( P8, Pigs), attr( P8, [eats-cucumbers, color-EatsCucumbers]),
length( Furs, N), maplist( color, Pigs, Furs),
writeln( Furs), writeln( EatsCucumbers), nl, !.
Testing:
14 ?- time(( pigs(_P), maplist(writeln,_P), ! )).
[black,brown,grey]
black
[color-black, sound-grumbles, eats-cucumbers|_G1484]
[color-brown, eats-carrots, sound-squeaks |_G1424]
[eats-salad, sound-giggles, color-grey |_G1463]
/* % 287 inferences, 0.000 CPU in 0.030 seconds (0% CPU, Infinite Lips) */
true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment