Prolog模擬社交圈

2022-05-19 15:14:33 字數 4875 閱讀 3968

社交圈中like是單向的,你like的人沒有義務like回來你。。。

這個實現關鍵難題是,不採用no邏輯,完全使用正邏輯來實現like, dislike等:

(cnblog也沒有prolog, 依舊使用scale代替,汗。。)

/* likes */

/* */

likes(g,x,y):-

member1(person(x,xs),g),

member1(y,xs).

/* member */

member1(x,[x|_]).

member1(x,[_|ys]):-

member1(x,ys).

member2(x,g):-

member1(person(x,_),g).

notmember(_,_,).

notmember(g,x,[y|ys]):-

notmember(g,x,ys),

different(g,x,y).

/*remove from list*/

remove1(x, [x|xs], xs).

remove1(x, [h|xs], [h|ys]) :-

remove1(x, xs, ys).

/*different*/

different(g,x,y):-

member2(x,g),

remove1(person(x, _), g, res),

member2(y,res).

/* dislikes */

/* */

dislikes(g,x,y):-

different(g,x,y),

likes(g,y,x),

member1(person(x,xs),g),

notmember(g,y,xs).

/*popular*/

popular(g,x):-

member1(person(x,xfriendlist),g),

alllikes(x,xfriendlist,g).

/* all elements in [x|xs] likes y */

alllikes(y,[x|xs],g):-

likes(g,x,y),

alllikes(y,xs,g).

alllikes(_,,_).

/*outcast*/

outcast(g,x):-

member1(person(x,xfriendlist),g),

alldislikes(x,xfriendlist,g).

/* all elements in [x|xs] dislikes y */

alldislikes(y,[x|xs],g):-

dislikes(g,x,y),

alldislikes(y,xs,g).

alldislikes(_,,_).

/*friendly*/

friendly(g,x):-

remove1(person(x, _), g, gg),

wholikes(g,person(x,_),gg,,res),

likesall(g,x,res).

/*wholikes*/

wholikes(_,_,,cont,cont).

wholikes(g,person(x,_),[person(y,_)|yys],cont,res):-

likes(g,y,x),

wholikes(g,person(x,_),yys,[y|cont],res).

wholikes(g,person(x,_),[person(_,ys)|yys],cont,res):-

notmember(g,x,ys),

wholikes(g,person(x,_),yys,cont,res).

/*likesall*/

likesall(_,_,).

likesall(g,x,[y|ys]):-

likes(g,x,y),

likesall(g,x,ys).

/*dislikesall*/

dislikesall(_,_,).

dislikesall(g,x,[y|ys]):-

dislikes(g,x,y),

dislikesall(g,x,ys).

/*hostile*/

hostile(g,x):-

remove1(person(x, _), g, gg),

wholikes(g,person(x,_),gg,,res),

dislikesall(g,x,res).

/*admires*/

admires(g,x,y):-

likes(g,x,y),

different(g,x,y).

admires(g,x,y):-

likes(g,x,z),

remove1(person(x,_),g,res),

admires(res,z,y).

notempty([_|_]).

isempty().

checkevery(_,_,,res1,res1).

checkevery(g,xs,[y|ys],res1,res2):-

member1(y,xs),

checkevery(g,xs,ys,res1,res2).

checkevery(g,xs,[y|ys],res1,res2):-

notmember(g,y,xs),

checkevery(g,xs,ys,[y|res1],res2).

checkgraph(g,x,_,,,):-

member1(person(x,),g).

checkgraph(g,_,_,lis1,lis2,lis1):-

notempty(lis1),

checkevery(g,lis1,lis2,,res),

isempty(res).

checkgraph(g,_,y,lis1,lis2,res):-

notempty(lis1),

checkevery(g,lis1,lis2,,res1),

notempty(res1),

member1(z,res1),

member1(person(z,zs),g),

checktoadd(g,zs,lis2,cont),

checkgraph(g,z,y,[z|lis1],cont,res).

checkgraph(g,x,y,lis1,lis2,res):-

isempty(lis1),

member1(person(x,xs),g),

checktoadd(g,xs,lis2,cont),

member1(z,xs),

notmember(g,z,lis1),

checkgraph(g,z,y,[x|lis1],cont,res).

checktoadd(_,,lis,lis).

checktoadd(g,[x|xs],lis,res):-

member1(x,lis),

checktoadd(g,xs,lis,res).

checktoadd(g,[x|xs],lis,res):-

member1(person(x,_),g),

notmember(g,x,lis),

checktoadd(g,xs,[x|lis],res).

notmemof(g,x,y):-

checkgraph(g,x,y,,,res),

notmember(g,y,res).

/*indifferent*/

indifferent(g,x,y):-

different(g,x,y),

notmemof(g,x,y).

/* same world */

comparelabel([x|xss],ys,label):-

member1((x,xs),label),

member1(xs,ys),

remove1(xs,ys,yss),

comparelabel(xss,yss,label).

comparelabel(,,_).

comparefriends(g,h,x,label):-

member1(person(x,xs),g),

member1((x,y),label),

member1(person(y,ys),h),

comparelabel(xs,ys,label).

allfriends(g,h,[x|xs],label):-

comparefriends(g,h,x,label),

allfriends(g,h,xs,label).

allfriends(_,_,,_).

same(g,h,a):-

getperson(g,x),

getperson(h,y),

combine(x,y,a).

combine(,,).

combine([x|xs],ys,[h|t]):-

member1(y,ys),pair(h,x,y),

remove1(y,ys,p),

combine(xs,p,t).

pair((x,y),x,y).

getperson(,).

getperson([person(x,_)|ys],[x|xs]):-getperson(ys,xs).

same_world(g,h,label):-

getperson(g,p),

same(g,h,label),

allfriends(g,h,p,label).

如何建立社交圈

在社交人脈領域有個通俗的說法 和你最親近的 10 個人決定你的人生高度 也有其他版本比如說是,4 個朋友或6個 值人之類的,本質上的道理沒什麼不同。雖然這個 經常被搞成功學的人利用去忽悠 去撈錢。但我從實際生活中,的確是切實感受到 你的人脈關係對你的生活 工作 事業 愛情的影響是很深遠的。中國有兩句...

A1107社交圈(並查集)

輸入n個人的愛好,愛好相同的在同乙個社交圈,跟書上的好朋友一樣,如果a b都喜歡活動1,b c都喜歡活動2,a和b在同一圈,b和c在同一圈,則a和c也在同一圈。跟書上一樣,書上直接給出朋友關係,本題需要先依靠hobby來判斷是否在同乙個集合,還有就是書上是直接用bool陣列判斷是根節點的個數,本題更...

建立社交圈交換經驗 心得,以求自我進步

在資訊化時代的今日,身為上班族,每天面對繁重的業務,若能建立良好的人際關係,就能確實掌握變化 擴張自我知訓領域,對於前途的發展自是裨益良多。除此之外,上班族如能借由社交使自己接觸不同性質的文明,即可激發本身的想像力及創造力,以正確分析各項資訊價值,並印讓自我訓練的成果。同時,追求進步的意願愈高,便往...