---
# System prepended metadata

title: Lista zagadnień nr 14 (na 09.06.2020)
tags: [mp20-dabi]

---

---
tags: mp20-dabi
---
# Lista zagadnień nr 14 (na 09.06.2020)
### Metody programowania 2020, II UWr

Grupa ćwiczeniowa: dabi 

---
### Ćwiczenie 1
::: info
Rozwiązuje: **Echo**
:::
```racket=
(define %grandson
  (%rel (a b c)
        [(a b)
         (%parent  a c)
         (%parent c b)
         (%male b)]))
         
Dla każdego a,b,c: (%parent a c) i (%parent c b) i (%male b)  => (%grandson a b)

Dla każdego a,b: (Jeżeli istnieje c: (%parent a c) i (parent c b)) i (%male b) => (%grandson a b)

 ;;przyklad zastosowania:        
> (%which ( a b) (%grandson a b))
   '((a . adam) (b . joshua))
> (%more)
   '((a . adam) (b . david))    
   
   (define %cousin
  (%rel (a b c d)
        [(a b)
         (%parent c a)
         (%parent d b)
         (%sibling c d)]))


(define %sibling
  (%rel (a b c)
        [(a b)
         (%parent c a)
         (%parent c b)
         (%/= a b)]))
                
 ;;przyklad zastosowania        
> (%which ( a b) (%cousin a b))
'((a . joshua) (b . joshua))
> (%more)
'((a . joshua) (b . joshua))
> (%more)
'((a . joshua) (b . david))
> (%more)

(define %is_mother
  (%rel (a b)
        [(a)
         (%parent a b)
         (%female a)]))
         
;;przykład użycia:
> (%which ( a) (%is_mother a))
'((a . eve))
> (%more)
'((a . eve))
> (%more)
'((a . eve))
> (%more)
'((a . helen))
> (%more)
'((a . ivonne))
> (%more)
#f


(define %is_father
  (%rel (a b)
        [(a)
         (%parent a b)
         (%male a)]))
         
;;przykład uzycia:
> (%which ( a) (%is_father a))
'((a . adam))
> (%more)
'((a . adam))
> (%more)
'((a . adam))
> (%more)
'((a . john))
> (%more)
'((a . mark))
> (%more)
#f

```
---
### Ćwiczenie 2
::: info
Rozwiązuje: **Echo**
:::
```racket=
 (%find-all () (%ancestor 'mark 'john ))
 ;;dostajemy:'(#f)
 
(%find-all (a) (%ancestor 'adam a))
;;dostajemy: '(((a . helen)) ((a . ivonne)) ((a . anna)) ((a . joshua)) ((a . david)))

(%find-all (a) (%sister 'ivonne a))
;;dostajemy: '(((a . helen)) ((a . ivonne)) ((a . anna)) ((a . helen)) ((a . ivonne)) ((a . anna)))

(%find-all (a b) (%cousin a b))
;;  '(((a . joshua) (b . joshua)) ((a . joshua) (b . joshua)) ((a . joshua) (b . david)) ((a . joshua) (b . david)) ((a . david) (b . joshua)) ((a . david) (b . joshua)) ((a . david) (b . david)) ((a . david) (b . david)))
```
---
### Ćwiczenie 3
::: info
Rozwiązuje: **Foxtrot**
:::
```racket=
> (%which (xs ys) (%my-append xs xs ys))
'((xs) (ys))
'((xs . ()) (ys . ()))
> (%more)
'((xs _) (ys _ _))
> (%more)
'((xs _ _) (ys _ _ _ _))
;; '((xs _G1 _G2) (ys _G1 _G2 _G1 _G2))
> (%which (x) (%select x '(1 2 3 4) '(1 2 4)))
'((x . 3))
> (%which (xs) (%my-append '(1 2 3) xs '(1 2 3 4 5)))
'((xs 4 5))
```
---
### Ćwiczenie 4
::: info
Rozwiązuje: **Alpha**
:::
```racket=
(define %my-append
  (%rel (ys x xs zs)
        [(null ys ys)]
        [((cons x xs) ys (cons x zs))
         (%my-append xs ys zs)]))

> (%which (xs ys) (%my-append xs ys '(1 2)))
'((xs) (ys 1 2))
> (%more)
'((xs 1) (ys 2))
> (%more)
'((xs 1 2) (ys))
> (%more)
#f
```
![](https://i.imgur.com/OzWNXLY.png)

```racket=

                         (%append xs ys '(1 2))
                       /                 |
             xs = '() /                  | xs = (cons x1 xs1)
            ys = ys1 /                   | ys = ys1
       '(1 2) = ys1 /                    | '(1 2) = (cons x1 zs1)
                   /                     |
                 :)                    (%append xs1 ys1 '(2))
              xs = '()                        /             \ 
              ys = '(1 2)                    / xs1 = '()     \ xs1 = (cons x2 xs2)
                                            / ys1 = ys2       \ ys1 = ys2
                                           / '(2) = ys2        \ '(2) = (cons x2 zs2)
                                          /                     \
                                        :)                   (%append xs2 ys2 '())
                                     xs = '(1)                 /               \
                                     ys = '(2)                / xs2 = '()       \ xs2 = (cons x3 xs3) 
                                                             / ys2 = ys3         \ ys2 = ys3
                                                            / '() = ys3           \ '() = (cons x3 zs3)
                                                           /                      :(
                                                         :)
                                                       xs = '(1 2)
                                                       ys = '()
```

---
### Ćwiczenie 5
::: info
Rozwiązuje: **Beta**
:::
```racket=
> (%which (x) (%= (list 'a x) (list 'a 'b)))
'((x . b))

>(%which (x y) (%= (list 'f (list 'g x) x) (list 'f y 'a)))
'((x . a) (y g a))

> (%which (x) (%= x (list 'f x)))
> (use-occurs-check? #t)

> (%which (x y) (%= x  (list 'g y)))
'((x g _) (y . _))

>(%which (x) (%= (list 'a)  (list 'a x)))
#f

>(%which (x) (%= (list 'a)  (cons 'a x)))
'((x))

>(%which (x y z) (%= (list 'a (list x x) (list y y)) (list x y z)))
'((x . a) (y a a) (z (a a) (a a)))
```
---
### Ćwiczenie 6
::: info
Rozwiązuje: **Charlie**
:::
```racket=
(define %sublist
 (%rel (xs ys y)
    [(null null)]
    [(xs (cons y ys))
     (%sublist xs ys)]
    [((cons y xs) (cons y ys))
     (%sublist xs ys)]))
     
> (%find-all (xs) (%sublist xs (list 1 2 3)))
'(((xs)) ((xs 3)) ((xs 2)) ((xs 2 3)) ((xs 1)) ((xs 1 3)) ((xs 1 2)) ((xs 1 2 3)))
```
---
### Ćwiczenie 7
::: info
Rozwiązuje: **Delta**
:::
```racket=
(define %permi
 (%rel (x xs ys zs)
   [(null null)]
   [((cons x xs) ys)
    (%permi xs zs)
    (%select x ys zs)]))
    
>(%find-all (xs) (%permi '(1 2 3) xs))
'(((xs 1 2 3)) ((xs 2 1 3)) ((xs 2 3 1)) ((xs 1 3 2)) ((xs 3 1 2)) ((xs 3 2 1)))

(define %perms
 (%rel (x xs ys zs)
  [(null null)]
  [(xs (cons x ys))
   (%select x xs zs)
   (%perms zs ys)]))
   
>(%find-all (xs) (%perms '(1 2 3) xs))
'(((xs 1 2 3)) ((xs 1 3 2)) ((xs 2 1 3)) ((xs 2 3 1)) ((xs 3 1 2)) ((xs 3 2 1)))
```
---
### Ćwiczenie 8
::: info
Rozwiązuje: **Wszyscy**
:::
```racket=
(define (list->num xs)
  (define (aux xs acc)
    (if (null? xs)
      acc
      (aux (cdr xs) (+ (* 10 acc) (car xs)))))
  (aux xs 0))
  
(define %send-more-money
  (%rel (D E M N O R S Y letters)
  [(D E M N O R S Y)
     (%permi (list D E M N O R S Y) letters)
     (%sublist letters '(0 1 2 3 4 5 6 7 8 9))
     (%=/=  M 0)
     (%=/=  S 0)
     (%is (+ (list->num (list S E N D)) (list->num (list M O R E))) (list->num (list M O N E Y)))]))
```