普莱舍写一个序言任务的解决方案 [英] Plese write a solution for a prolog task

查看:142
本文介绍了普莱舍写一个序言任务的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵 M×N的,我需要切换元素的地方与索引 [1,N] [M,N]

更新

我是真正的新Prolog的,这里是我的解决方案,它返回false:(

 主([FIRSTROW |尾],X): - 
    去年(FIRSTROW,A),
    最后(尾,LASTROW)
    去年(LASTROW,B),
    skipLastItem(FIRSTROW,FirstRowWithoutA)
    skipLastItem(LASTROW,LastRowWithoutB)
    追加(FirstRowWithoutA,[B],FirstRowNew),
    追加(LastRowWithoutB,[A],LastRowNew)
    分配([FirstRowNew],X)
    skipLastItem(尾部,中)
    appendAllElements(中间,X)
    追加(X,LastRowNew,X)。appendAllElements([X |尾],列表): -
    追加(列表中,X,NewList),
    appendAllElements(尾,NewList)。appendAllElements([],_)。分配(项目,项)。skipLastItem([_],[]): - !。
skipLastItem([H | T],[H | S): -
skipLastItem(T,S)。


解决方案

这听起来像功课,所以我要在这里有点模糊...

开始用列表替换一个值的简单问题。写一个递归predicate

  swap_list(X,N,A,B,Y)

应被理解为一个列表X中,在位置N,除去值A和硼替代它给列表Y

现在我们可以将该延伸到基质的情况。写第二递归predicate

  swap_matrix(X,M,N,A,B,Y)

应被理解为一个矩阵X,在位置(M,N),除去元素A和硼替代它给出了矩阵Y。这个递归,其中 M = 0 ,将包含一个电话 swap_list 的基本情况。

现在,可以用下面的交换两个位置(M1,N1)和(M 2,N 2):

 互换(X,M1,N1,M2,N2,Y): - 
    swap_matrix(X,M1,N1,A,B,Z)
    swap_matrix(Z,M2,N2,B,A,Y)。

请注意,我们插入 B 到矩阵以Z 之前,我们甚至知道它是什么 - B不是分配一个值,直到第二个 swap_matrix 电话。

I have a matrix M x N, I need to switch places of the elements with indexed [1,N] and [M,N].

Update

I am really new to Prolog, here is my solution that returns false :(

main([FirstRow|Tail],X):-
    last(FirstRow, A),
    last(Tail, LastRow),
    last(LastRow, B),
    skipLastItem(FirstRow,FirstRowWithoutA),
    skipLastItem(LastRow,LastRowWithoutB),
    append(FirstRowWithoutA,[B],FirstRowNew),
    append(LastRowWithoutB,[A],LastRowNew),
    assign([FirstRowNew],X),
    skipLastItem(Tail,Middle),
    appendAllElements(Middle,X),
    append(X,LastRowNew,X).

appendAllElements([X|Tail],List):-
    append(List,X,NewList),
    appendAllElements(Tail,NewList).

appendAllElements([],_).

assign(Item,Item).

skipLastItem([_],[ ]) :- !.
skipLastItem([H|T],[H|S]) :-
skipLastItem(T,S).

解决方案

This sounds like homework, so I'm going to be a bit vague here...

Start with the simpler problem of replacing one value in a list. Write a recursive predicate

swap_list(X,N,A,B,Y)

which should be read as "for a list X, at position N, removing the value A and replacing it with B gives the list Y".

Now we can extend this to the case of matrices. Write a second recursive predicate

swap_matrix(X,M,N,A,B,Y)

which should be read as "for a matrix X, at position (M,N), removing element A and replacing it with B gives the matrix Y". The base case of this recursion, where M=0, will contain a call to swap_list.

Now, you can swap two positions (M1,N1) and (M2,N2) with the following:

swap(X,M1,N1,M2,N2,Y) :-
    swap_matrix(X,M1,N1,A,B,Z),
    swap_matrix(Z,M2,N2,B,A,Y).

Note that we insert B into the matrix Z before we even know what it is - B isn't assigned a value until the second swap_matrix call.

这篇关于普莱舍写一个序言任务的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆