prolog

Converting minutes to hours in prolog

岁酱吖の 提交于 2020-03-05 00:25:22
问题 I am trying to convert total minutes(N) to number of hours(H) and number of minutes(M) in prolog using this code (Have not implemented counting the minutes yet): minutes_to_hours(N, H, M) :- ( N >= 60 -> H is H1+1, N is N1-60, minutes_to_hours(N, H, M) ; writeln(H) ). I get this error: Arguments are not sufficiently instantiated In: [2] _1440 is _1446+1 [1] mins_to_hours_and_mins(60,_1508,_1510) at line 1 回答1: As you requested, a possible solution using recursion could be: minutes_to_hours

Solving a puzzle in Prolog about time constraints

家住魔仙堡 提交于 2020-03-03 07:35:27
问题 Stuck on a Prolog problem. I know the answer (because I did it on paper first), but I cannot figure out how to get Prolog to come up with the answer. Problem: Bill eats a snack every night, having a different fruit and different nuts each night. From the statements below, identify what Bill had for a snack for each weeknight last week. a) The apple was eaten later in the week than the mango. b) The banana was eaten later in the week than both the almonds and peanuts, but earlier in the week

swi-prolog 解决数独

做~自己de王妃 提交于 2020-02-29 19:41:31
swi-prolog是prolog比较流行的一种实现,但话说prolog这货的语法还真不好理解,而且资料少,当年与LISP相抗衡,现在好像不怎么火了。在win7上折腾,还是没把命令行交互模式下的readline补全给搞明白,体验太差了。 事实与规则文件内容如下: % 加载库--有限域的计算 :- use_module(library(clpfd)). valid([]). valid([Head|Tail]) :- all_different(Head), % in the book, this is 'fd_all_different' valid(Tail). % beginning of sudoku rule itself sudoku(Puzzle, Solution) :- Solution = Puzzle, Puzzle = [S11, S12, S13, S14, S21, S22, S23, S24, S31, S32, S33, S34, S41, S42, S43, S44], Puzzle ins 1..4, % in the book, this is 'fd_domain' Row1 = [S11, S12, S13, S14], Row2 = [S21, S22, S23, S24], Row3 = [S31, S32, S33, S34],

用Prolog解决数独问题

房东的猫 提交于 2020-02-29 18:45:02
我们先了解一下什么是数独 数独( shù dú )是源自 18 世纪瑞士的一种数学游戏。它是一种运用纸、笔进行演算的逻辑游戏。数独有多种类型,我们仅以其中一种类型作为本文实例。 玩家需要根据 9×9 盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个粗线宫( 3*3 )内的数字均含 1-9 ,不重复。 方格 水平方向有九横行,垂直方向有九纵列的矩形,画分八十一个小正方形,称为九宫格( Grid ),如图一所示,是数独( Sudoku )的作用范围。 行:水平方向的每一横行有九格,每一横行称为行(Row) 列:垂直方向的每一纵列有九格,每一纵列称为列( Column ) 宫:三行与三列相交之处有九格,每一单元称为小九宫( Box 、 Block ),简称 宫 ,如图四所示(在杀手数独中,宫往往用单词 Nonet 表示) 上述行、列、宫、单元格统称为单元( Unit );而行、列、宫统称为区域( Region )。 区块 由三个连续宫组成大行列( Chute ),分大行( Floor )及大列( Tower )。 第一大行:由第一宫、第二宫、第三宫组成。 第二大行:由第四宫、第五宫、第六宫组成。 第三大行:由第七宫、第八宫、第九宫组成。 第一大列:由第一宫、第四宫、第七宫组成。 第二大列:由第二宫、第五宫、第八宫组成。 第三大列:由第三宫、第六宫、第九宫组成。

Multiple instances of Prolog clauses with identical ground head. Any uses besides for this besides (dubious) control of computation?

那年仲夏 提交于 2020-02-25 02:10:11
问题 In SWI Prolog: Exact clause duplicates are allowed: a(1). a(1). ?- a(X). X = 1 ; X = 1. or even: c :- format("Hello from first c!"). c :- format("Hello from second c!"). Hello from first c! true ; Hello from second c! true. More generally, so are clauses with identical fully ground heads but differing bodies: b(1) :- format("Hello from first b!"). b(1) :- format("Hello from second b!"). ?- b(1). Hello from first b! true ; Hello from second b! true. Clauses with identical non-ground head feel

Prolog list to comma separated string

£可爱£侵袭症+ 提交于 2020-02-23 15:59:10
问题 I have a list like [apple, orange] and I want to convert it to a string like "apple,orange" in Prolog. Do you have any idea? 回答1: In SWI-Prolog you can simply use atomic_list_concat/3 and atom_string/2 : ?- atomic_list_concat([apple, banana, oranges], ',', Atom), atom_string(Atom, String). Atom = 'apple,banana,oranges', String = "apple,banana,oranges". 回答2: In SWI-Prolog you can use with_output_to/2 . Below are two versions, one using write/1 and the other writeq/1 . It's not clear from your

Converting a list into a term in Prolog

☆樱花仙子☆ 提交于 2020-02-20 09:21:27
问题 What is the best way of converting a Prolog list into a Prolog term (that is not a list), in terms of efficiency, and using existing built-in predicates as much as possible? The interface and usage examples would be the following. %% list_to_term(+List:list, +Functor:atom, -Term:term) % % Usage: % % ?- list_to_term([], myfunctor, Term). % Term = myfunctor. % % ?- list_to_term([a, b, [c], D, 2], myfunctor, Term). % Term = myfunctor(a, b, [c], D, 2). I.e. the given list (which is actually a

Recursive program to compute Taylor approx of cosine not functioning in Prolog

这一生的挚爱 提交于 2020-02-15 10:25:45
问题 I'm still pretty new to Prolog, and i'm not sure why this code isn't working. I believe it is most likely a problem with the base case or in the last 3 lines of the recursive case. Everything else works just fine. This program determines cosine calculated with series approximation, to do so it needs to calculate the factorial of 2K, also -1 ^ K, and then uses these 2 calculations in the final equation (this is done in % Recursive Case). % Factorial from class fact(0, 1). fact(N, F) :- N > 0,

Displaying all the members of database in Prolog

瘦欲@ 提交于 2020-02-06 08:08:40
问题 I'm trying to make a predicate "find_recipe" which fetches a list of ingredients based on the name of the recipe, given by the user. I defined 3 recipes and am trying to first show a prompt for a user, let the user enter the recipe name, and then next few lines which are commented out should handle fetching the designated recipe, and lastly the line beginning with format will print out the result. recipe('Makaronilaatikko', ['macaroni', 'potato', 'onion', 'cheese', 'milk', 'egg', 'minced meat

Displaying all the members of database in Prolog

孤街醉人 提交于 2020-02-06 08:06:07
问题 I'm trying to make a predicate "find_recipe" which fetches a list of ingredients based on the name of the recipe, given by the user. I defined 3 recipes and am trying to first show a prompt for a user, let the user enter the recipe name, and then next few lines which are commented out should handle fetching the designated recipe, and lastly the line beginning with format will print out the result. recipe('Makaronilaatikko', ['macaroni', 'potato', 'onion', 'cheese', 'milk', 'egg', 'minced meat