Bài tập prolog vị từ

Embed Size (px)

Citation preview

  • 8/17/2019 Bài tập prolog vị từ

    1/16

    LISP LIST trả về danh sách các đối sốCONS thêm một phần tử vào đầu danh sáchAPPEND kết hợp các phần tử của mọi danh sách đã choNTHCDR ct n phần tử đầu danh sách! v"i th#n$ số đầu ch% số phần tử cần ct* (setq l ‘(a b c d e))(a b c d e)* (nthcdr 2 l)(c d e)

    BUTLAST ct n phần tử cuối danh sách! v"i th#n$ số đầu &à danh sách! th#n$ số

    th' hai ch% số phần tử cần ct* (setq l ‘(a b c d e))(a b c d e)* (butlast l 2)(a b c)* (butlast l 10)NIL

    LAST trả về danh sách t(t cả phần tử tr) phần tử cuối c*n$ đã +, &o-i ra* (setq l ‘(a b c d e)l1 ‘((a b) (c d)))((a b) (c d))* (last l)

    (e)* (last l1)((c d))

    LENGTH trả về chiều dài của chu.iREVERSE trả về chu.i n$h,ch đảoprogn/progn 01 222 0n3đánh $iá tuần t4 các +i5u th'c 01! 222! 0n t) trái san$ phải và kết 6uả trả về &à $iá tr, của

     +i5u th'c 0nprog1/prog1 01 222 0n3

    đánh $iá tuần t4 các +i5u th'c 01! 222! 0n t) trái san$ phải và kết 6uả trả về &à $iá tr, của +i5u th'c 01

  • 8/17/2019 Bài tập prolog vị từ

    2/16

    Câu 1: Đếm số phần tử trong list(defun dem(L)

      (  if (null L) 0 (+ 1 (dem (cdr L)))  ))Câu 2: tìm phần tử thứ n:(defun ptu_n (L n)

    (  if (= n 1) (car L) (ptu_n (cdr L) (- n 1))

    ))Câu 3: lọc số dương trong dnh s!ch " (defun locdsduong(L)

  • 8/17/2019 Bài tập prolog vị từ

    3/16

    (cond((null L) nil )((>= (car L) 0) (cons (car L) (locdsduong (cdr L))))((< (car L) 0) (locdsduong (cdr L)))

    ))note l!c s" #m t$% &'i du > t$n$ <

    Câu#: 2 ds $ %& '( lọc r ds ' c) nh*ng pt ch+ co trong $ m& , c) trong '

    (defun su*list(L P) (cond ((null L) nil)  ((mem*er (car L) P) (su*list (cdr L) P))  ((cons (car L) (su*list (cdr L) P)))))

    Câu 5. Tính tng !"! ph#n t$ !%& '()t(defun sumlist (L)  (if (null L)  0  (+ (first L) (sumlist (rest L)))  ))

     

    âu 6. Tính tổng hai phần tử lớn nhất của list

    (defun sum2biggest (L)  (sumlist (2biggest L))

    ) (defun 2biggest (L)  (if (= (length L) 1)  L  (prgn  (setq remain (2biggest (rest L)))  (if (! (first L) (first remain))  (list (first L) (first remain))  (if (r (null (secnd remain)) (! (first L)(secnd remain)))  (list (first remain) (first L))

      remain  )  )  )  ))

     

    Câu 7. Tr v m ng 2 chi u ch a chu i tăng trong listả ề ả ề ứ ỗ(defun asclists (L)  (if (= (length L) 1)  (list L)  (prgn  (setq remain (asclists (rest L)))

  • 8/17/2019 Bài tập prolog vị từ

    4/16

      (if (" (first L) (first (first remain)))  (cns (cns (first L) (first remain)) (restremain))  (cns (list (first L)) remain)  )  )  ))

     

    Câu - ,t m.t list t$n$ / p$n &. *i list &u &23c c$o tr24c5$6ng s7 d8ng c9c $m c: s;nample

      (split ?(a * c d e f g $ i @) A)

      ( (B C ,) (D E F G I 5))

    (defun split (list count)

      (list (su*seH list 0 count)

      (nt$cdr count list)))Câu . :a p$n t7 t$J @ ra @$Ki dan$ s9c$

    (defun rem#e$at (list inde%)

      (cnd

      ((" inde% 1) (errr &In#alid inde%&))

      ((= inde% 1) (rest list))

      (t (cns (first list) (rem#e$at (rest list) (1$inde%))))))

    ,#u 10 $m m.t p$n t7 Mo list tNi MO tr inde

    QD

      (insert-at ?alfa ?(a * c d) /)  (B BLEB C , D)

    (defun insert$at (item list inde%)

      (append (subseq list 0 (1$ inde%))

      (list item)

      (nthcdr (1$ inde%) list)))

  • 8/17/2019 Bài tập prolog vị từ

    5/16

    R.t s" dNng *' sungo9 p$n t7 t$J n(defun oan (L n)(

    if (= n 0) (cdr L) (cons (car L) (oan (cdr L) (- n 1)))))&o c$uTi(defun daoc$uoi(L)  (sin$c$uoidao L () ))(defun sin$c$uoidao (L C)

    (if (null L)  C  (sin$c$uoidao (cdr L) (cons (car L) C ))))

    $m p$n t7 Mo MO tr t$J n(defun t$emptu_Maon(ptu L n)(if (or (eHl n 1) (null L))

    (cons ptu L)

      (cons (car L) (t$emptu_Maon ptu (cdr L) (- n 1) ))))tNo list tU p$#n t7 &u M p$n t7 cu"i(defun range (ini fim)  (if (> ini fim)  VV Se a lista W de um nXmero maior para um menor VV  (if (eHl ini fim)  (cons fim nil)  (cons ini (range (- ini 1) fim)))

      VV Se a lista W de um nXmero menor para um maior VV  (if (eHl ini fim)  (cons fim nil)  (cons ini (range (+ ini 1) fim)))))t' $3p c$Yp @ p$n t7(defun com*ination (n-elem org-list)  (let ((num-elem (- n-elem 1)))  (if (or (> num-elem (lengt$ org-list)) (< n-elem /))  nil  (let ( (elem-list (split num-elem org-list)) (rest-list (split-after num-elem org-list)))  (append (com*ination-lista-elem elem-list rest-list) (com*ination n-elem (cdr org-

  • 8/17/2019 Bài tập prolog vị từ

    6/16

    list)))))))

    (defun com*ination-lista-elem (elem-list org-list)  (if (eHl org-list nil)  nil

      (append (list (append elem-list (list (car org-list)))) (com*ination-lista-elem elem-list (cdrorg-list)))))

     

    PrologCi to9n &ong n24c AZ[\ c$ !n ra ]^---------------Eull BC,-----------f(_C,`b`)- is A is C b is ,\

    f(B_,`b`)- is B is Z b is ,\f(BC_`b`)- is B is C b is [\^---------------fill BC,-----------f(_C,`b`)- is 0 is C b is ,\f(B_,`b`)- is B is 0 b is ,\f(BC_`b`)- is B is C b is 0\^ B->Cf(BC,`b`)- B+C>Z is Z is B+C-Z b is ,\f(BC,`b`)- B+C=B

    f(BC,`b`)- B+C>A is A is B+C-A b is ,\f(BC,`b`)- B+C=,f(BC,`b`)- C+,>[ b is [ is C+,-[ is B\f(BC,`b`)- C+,=Cf(BC,`b`)- C+,>Z is Z b is C+,-Z is B\f(BC,`b`)- C+,=,f(BC,`b`)- B+,>[ b is [ is B+,-[ is C\

    f(BC,`b`)- B+,=B

  • 8/17/2019 Bài tập prolog vị từ

    7/16

    f(BC,`b`)- B+,>A is Ab is B+,-A is C\f(BC,`b`)- B+,=

  • 8/17/2019 Bài tập prolog vị từ

    8/16

     gal('1111)

     

    %chu*+n t)ng côn t) bờ b,n t#-i sang ph(if(',-./'11./)$,=0-=0f(',-./'1-1/)$,=0.=0f(',-./'1-.1)$,=0/=0

     

    %chu*+n t)ng côn t) bờ b,n ph(i sang t#-if(',-./'00./)$,=1-=1f(',-./'0-0/)$,=1.=1f(',-./'0-.0)$,=1/=1

     8thu:;n

  • 8/17/2019 Bài tập prolog vị từ

    9/16

    XchuFYn A 6uF2Z/T[!8!:!\!@W!T[1!81!:1!\1!@1W37] @^U!8_^A![1 is [! 81 is 8]A!:1 is :!\1 is \`A!@1 is 12Z/T[!8!:!\!@W!T[1!81!:1!\1!@1W37] @^1!\_^A![1 is [! 81 is 8`A!:1 is :!\1 is \]A!

    @1 is U2XchuFYn thuFYn 6ua &aiZ/T[!8!:!\!@W!T[1!81!:1!\1!@1W37] @^1!@1 is U![1 is [! 81 is 8! :1 is :! \1 is \2Z/T[!8!:!\!@W!T[1!81!:1!\1!@1W37] @^U!@1 is 1![1 is [! 81 is 8! :1 is :! \1 is \2condition/T[!8!:!\!@W37]@^1!8_[2condition/T[!8!:!\!@W37]@^U!\_:2dZs/:odY!bath!T:odYbathW37]$oa&/:odY3!2dZs/:odY!:bath!bath37]Z/:odY!e3!:odYf^e!not/mYm+Yr/e!:bath33!not/condition/e33!dZs/e!T:odY

    :bathW!bath32 

    vYrsion A28 ?epresent a state as '.L@L-.?@?gal('00right) legal(.L@L.?@?) $  8 is this state a legal neA

      @L!=0 .L!=0 @?!=0 .?!=0  (@L!=.L > @L=0)  (@?!=.? > @?=0) 8 3ssible m#esm#e('.L@Lleft.?@?'.L@L2right.?@?2)$  8 BC missinaries crss left t right  @?2 is @?+2  @L2 is @L$2

      legal(.L@L2.?@?2) m#e('.L@Lleft.?@?'.L2@Lright.?2@?)$  8 BC cannibals crss left t right  .?2 is .?+2  .L2 is .L$2  legal(.L2@L.?2@?) m#e('.L@Lleft.?@?'.L2@L2right.?2@?2)$

  • 8/17/2019 Bài tập prolog vị từ

    10/16

      8 Dne missinar: and ne cannibal crss left tright  .?2 is .?+1  .L2 is .L$1

      @?2 is @?+1  @L2 is @L$1  legal(.L2@L2.?2@?2) m#e('.L@Lleft.?@?'.L@L2right.?@?2)$  8 Dne missinar: crsses left t right  @?2 is @?+1  @L2 is @L$1  legal(.L@L2.?@?2)

     m#e('.L@Lleft.?@?'.L2@Lright.?2@?)$  8 Dne cannibal crsses left t right  .?2 is .?+1  .L2 is .L$1  legal(.L2@L.?2@?) m#e('.L@Lright.?@?'.L@L2left.?@?2)$  8 BC missinaries crss right t left

      @?2 is @?$2  @L2 is @L+2  legal(.L@L2.?@?2) m#e('.L@Lright.?@?'.L2@Lleft.?2@?)$  8 BC cannibals crss right t left  .?2 is .?$2  .L2 is .L+2  legal(.L2@L.?2@?)

     m#e('.L@Lright.?@?'.L2@L2left.?2@?2)$  8 Dne missinar: and ne cannibal crss right tleft  .?2 is .?$1  .L2 is .L+1  @?2 is @?$1  @L2 is @L+1  legal(.L2@L2.?2@?2)

     m#e('.L@Lright.?@?'.L@L2left.?@?2)$

  • 8/17/2019 Bài tập prolog vị từ

    11/16

      8 Dne missinar: crsses right t left  @?2 is @?$1  @L2 is @L+1  legal(.L@L2.?@?2)

     m#e('.L@Lright.?@?'.L2@Lleft.?2@?)$  8 Dne cannibal crsses right t left  .?2 is .?$1  .L2 is .L+1  legal(.L2@L.?2@?) dfs(.Nde.3ath'.Nde4.3ath)$gal(.Nde)5dfs(.Nde.3ath3ath)$f(.Nde6).Nde7=67

    +member(6.3ath)dfs(6'.Nde4.3ath3ath) dfs([3,3,left,0,0],Path,[]).

     

    -B*( to"n 23 !h4ng ,u& )/ng0Vr)(on 1

    gal('1111111)

    81 NEFDI dim#e(',1-1,2-2,-6'B-1,2-2,-B)$(,1 =6)B is 1 $ ,1m#e(',1-1,2-2,-6',1B,2-2,-B)$(-1 =6)B is 1 $ -1m#e(',1-1,2-2,-6',1-1B-2,-B)$(,2 =6)B is 1 $ ,2m#e(',1-1,2-2,-6',1-1,2B,-B)$(-2 =6)B is 1 $ -2m#e(',1-1,2-2,-6',1-1,2-2B-B)$(, =6)B is 1 $ ,m#e(',1-1,2-2,-6',1-1,2-2,BB)$(- =6)B is 1 $ - 82 .GDNE /Im#e(',1-1,2-2,-6'B-1B-2,-B)$(,1 =6)(,1 = ,2)B is 1 $ ,1

    m#e(',1-1,2-2,-6'B-1,2-2B-B)$(,1 =6)(,1 = ,)B is 1 $ ,1

  • 8/17/2019 Bài tập prolog vị từ

    12/16

    m#e(',1-1,2-2,-6',1-1B-2B-B)$(,2 =6)(,2 = ,)B is 1 $ ,2 82 HD /I

    m#e(',1-1,2-2,-6',1B,2B,-B)$(-1 =6)(-1 = -2)B is 1 $ -1m#e(',1-1,2-2,-6',1B,2-2,BB)$(-1 =6)(-1 = -)B is 1 $ -1m#e(',1-1,2-2,-6',1-1,2B,BB)$(-2 =6)(-2 = -)B is 1 $ -28.GDNE H, HDm#e(',1-1,2-2,-6'BB,2-2,-B)$(,1 =6)(,1 = -1)B is 1 $ ,1

    m#e(',1-1,2-2,-6',1-1BB,-B)$(,2 =6)(,2 = -2)B is 1 $ ,2m#e(',1-1,2-2,-6',1-1,2-2BBB)$(, =6)(, = -)B is 1 $ ,#alidperatr(',1-1,2-2,-)$((,1=-1)>((,17=-1)(,1=,2)(,2=,)))  (,2=-2>((,27=-2)(,1=,2)(,2=,)))  (,=->((,7=-)(,1=,2)(,2=,)))

     J: lK cc trMng hOp

  • 8/17/2019 Bài tập prolog vị từ

    13/16

    %7 ng$ời /ua sông

    %C-c c8p !c lần l$2t /ua sôngf(',-./RS6'B-.?RST)$ 6=,6=/B is 1 $ ,? is 1 $ /Tis 1 $ 6 8 nUu , / cVng (L1!

    0listsplit(,GB)listsplit(-G-)G-!G))

  • 8/17/2019 Bài tập prolog vị từ

    14/16

    %chu*+n t) sang f(',-.'B-'G4.)$length(,L) L!0 length(.L1)((L1 is 0listsplit(,GB))> (L1! 0listsplit(,GB)listsplit(.G.)G.!G))

    %chu*+n t) 7 sang f(',-.''G4,B.)$length(-L)L!0 length(,L1)((L1 is 0listsplit(-GB))> (L1!0 listsplit(-GB)listsplit(,G,)G,!G))

    %chu*+n t) 7 sang f(',-.',B'G4.)$length(-L)L!0 length(.L1)((L1 is 0listsplit(-GB))> (L1!0 listsplit(-GB)listsplit(.G.)G.!G))

    %chu*+n t) sang f(',-.''G4,-B)$length(.L)L!0 length(,L1)((L1 is 0listsplit(.GB))> (L1!0 listsplit(.GB)listsplit(,G,)G,!G))

    %chu*+n t) sang 7f(',-.','G4-B)$length(.L)L!0 length(-L1)((L1 is 0listsplit(.GB))> (L1!0 listsplit(.GB)listsplit(-G-)G-!G))dfs(.Nde.3ath'.Nde4.3ath)$gal(.Nde)5dfs(.Nde.3ath3ath)$f(.Nde6).Nde7=6 7+member(6.3ath)dfs(6'.Nde4.3ath3ath)

    '&i Đuốcphi5n 67n 1gal('11111)f(',-./B6'\-./\61)$(B=,) \ is 1$B61 is 6+1

    f(',-./B6',\./\61)$(B=-) \ is 1$B61 is 6+2f(',-./B6',-\/\61)$(B=.) \ is 1$B61 is 6+Qf(',-./B6',-.\\61)$(B=/) \ is 1$B61 is 6+]

    %A i !ới &0C04f(',-./B6'\\./\61)$(B=,)(B=-) \ is 1$B61 is 6+2f(',-./B6'\-\/\61)$(B=,)(B=.) \ is 1$B61 is 6+Qf(',-./B6'\-.\\61)$(B=,)(B=/) \ is 1$B61 is 6+]

    %& i !ới C0 4

    f(',-./B6',\\/\61)$(B=-)(B=.) \ is 1$B61 is 6+Qf(',-./B6',\.\\61)$(B=-)(B=/) \ is 1$B61 is 6+]

  • 8/17/2019 Bài tập prolog vị từ

    15/16

    %C i !ới 4f(',-./B6',-\\\61)$(B=.)(B=/) \ is 1$B61 is 6+]

    checZ('61)$1Q!=61dfs(.Nde.3ath'.Nde4.3ath)$gal(.Nde)5dfs(.Nde.3ath3ath)$f(.Nde6).Nde7=67+member(6.3ath)checZ(6)dfs(6'.Nde4.3ath3ath)

    -------------------------------------hu"c-------------------------------------------p$in *n /gal('11111)81 ngui dim#e(',-./6',1-./IB)$(,=6)(,1 is (1 $ 6))(I is ,1)Bis 1

    m#e(',-./6',-1./IB)$(-=6)(-1 is (1 $ 6))(I is -1)Bis 2m#e(',-./6',-.1/IB)$(.=6)(.1 is (1 $ 6))(I is .1)Bis Qm#e(',-./6',-./1IB)$(/=6)(/1 is (1 $ 6))(I is /1)Bis ]82 ngui di81$!2Q]m#e(',-./6',1-1./IB)$(,=6)(-=6)(,1 is (1$6))(-1 is,1)(I is ,1)B is 2

    m#e(',-./6',1-.1/IB)$(,=6)(.=6)(,1 is (1$6))(.1 is,1)(I is ,1)B is Qm#e(',-./6',1-./1IB)$(,=6)(/=6)(,1 is (1$6))(/1 is,1)(I is ,1)B is ]82$!Q]m#e(',-./6',-1.1/IB)$(-=6)(.=6)(-1 is (1$6))(.1 is-1)(I is -1)B is Qm#e(',-./6',-1./1IB)$(-=6)(/=6)(-1 is (1$6))(/1 is-1)(I is -1)B is ]8Q$!]m#e(',-./6',-.1/1IB)$(.=6)(/=6)(.1 is (1$6))(/1 is.1)(I is .1)B is ]

    dfs(.Nde.3ath'.Nde4.3ath^)$gal(.Nde)(1Q != ^)5dfs(.Nde.3ath3ath^)$m#e(.Nde6B)^1 is (^ + B).Nde7=67+member(6.3ath)  dfs(6'.Nde4.3ath3ath^1)

    ,#u Huer = dfs(00000``0)lengt$()\ 9c n$Yn l cn j *24c &i l &k\ 1] p$Xt lmin\

    ------------------------------------------------Ganoioer-----------------------------------

  • 8/17/2019 Bài tập prolog vị từ

    16/16

    t$9p $ n.i p$in *n cao cp

    goal(AAA`)\^c$uen dia 1\moMe(BC,`B1C,`)-(B = 1)(B1 is 1)\

    moMe(BC,`B1C,`)-(B = /)(B1 is /)\moMe(BC,`B1C,`)-(B = A)(B1 is A)\^c$uen dia /\moMe(BC,`BC1,`)-(C = B)(B = 1)(C1 is /)\moMe(BC,`BC1,`)-(C = B)(B = 1)(C1 is A)\moMe(BC,`BC1,`)-(C = B)(B = /)(C1 is 1)\moMe(BC,`BC1,`)-(C = B)(B = /)(C1 is A)\moMe(BC,`BC1,`)-(C = B)(B = A)(C1 is 1)\moMe(BC,`BC1,`)-(C = B)(B = A)(C1 is /)\^c$uen dia A\moMe(BC,`BC,1`)-(, = B)(,=C)(B = 1)(C = /)(,1 is A)\moMe(BC,`BC,1`)-(, = B)(,=C)(B = /)(C = 1)(,1 is A)\

    moMe(BC,`BC,1`)-(, = B)(,=C)(B = 1)(C = A)(,1 is /)\moMe(BC,`BC,1`)-(, = B)(,=C)(B = A)(C = 1)(,1 is /)\

    moMe(BC,`BC,1`)-(, = B)(,=C)(B = 1)(C = 1)(,1 is /)\moMe(BC,`BC,1`)-(, = B)(,=C)(B = 1)(C = 1)(,1 is A)\

    moMe(BC,`BC,1`)-(, = B)(,=C)(B = /)(C = /)(,1 is 1)\moMe(BC,`BC,1`)-(, = B)(,=C)(B = /)(C = /)(,1 is A)\

    moMe(BC,`BC,1`)-(, = B)(,=C)(B = A)(C = A)(,1 is /)\moMe(BC,`BC,1`)-(, = B)(,=C)(B = A)(C = A)(,1 is 1)\

    dfs(,ode,Pat$,ode,Pat$`)-goal(,ode)\dfs(,ode,Pat$Pat$)-moMe(,ode),ode=+mem*er(,Pat$)  dfs(,ode,Pat$`Pat$)\