fork download
  1. % 商品データ
  2. % product(商品名, 価格, 評価, レビュー数).
  3.  
  4. product(item1, 3000, 4.8, 20).
  5. product(item2, 1800, 4.2, 200).
  6. product(item3, 5000, 4.9, 5).
  7. product(item4, 2500, 4.5, 80).
  8.  
  9. % 価格が3000円以下の商品
  10. cheap_product(Name, Price) :-
  11. product(Name, Price, _, _),
  12. Price =< 3000.
  13.  
  14. % 評価が4.5以上の商品
  15. high_rating_product(Name, Rating) :-
  16. product(Name, _, Rating, _),
  17. Rating >= 4.5.
  18.  
  19. % レビュー数が50件以上の商品
  20. popular_product(Name, Reviews) :-
  21. product(Name, _, _, Reviews),
  22. Reviews >= 50.
  23.  
  24. % 評価が高く、レビュー数も多いおすすめ商品
  25. recommended_product(Name) :-
  26. product(Name, _, Rating, Reviews),
  27. Rating >= 4.5,
  28. Reviews >= 50.
  29.  
  30. % Ideoneで実行するためのmain
  31. main :-
  32. write('3000円以下の商品:'), nl,
  33. forall(
  34. cheap_product(Name, Price),
  35. (
  36. write(Name),
  37. write(' 価格: '),
  38. write(Price),
  39. )
  40. ),
  41. nl,
  42.  
  43. write('評価が4.5以上の商品:'), nl,
  44. forall(
  45. high_rating_product(Name2, Rating),
  46. (
  47. write(Name2),
  48. write(' 評価: '),
  49. write(Rating),
  50. )
  51. ),
  52. nl,
  53.  
  54. write('レビュー数が50件以上の商品:'), nl,
  55. forall(
  56. popular_product(Name3, Reviews),
  57. (
  58. write(Name3),
  59. write(' レビュー数: '),
  60. write(Reviews),
  61. )
  62. ),
  63. nl,
  64.  
  65. write('おすすめ商品:'), nl,
  66. forall(
  67. recommended_product(Name4),
  68. (
  69. write(Name4),
  70. )
  71. ).
  72.  
  73. :- initialization(main).
Success #stdin #stdout #stderr 0.02s 7012KB
stdin
Standard input is empty
stdout
3000円以下の商品:
item1 価格: 3000
item2 価格: 1800
item4 価格: 2500

評価が4.5以上の商品:
item1 評価: 4.8
item3 評価: 4.9
item4 評価: 4.5

レビュー数が50件以上の商品:
item2 レビュー数: 200
item4 レビュー数: 80

おすすめ商品:
item4
stderr
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit