---
# System prepended metadata

title: MP 9
tags: [mp]

---

---
tags: mp
---

# MP 9

## mutable.rkt
```=
#lang racket

(define (cycle xs)
  (define (list->mlist xs)
    (cond [(null? xs) null]
          [else (mcons (car xs) (list->mlist (cdr xs)))]))
  (define (first-to-last! mxs)
    (define (aux mys)
      (cond [(null? mys) (error "Can't cycle empty list")]
            [(and (mpair? mys) (null? (mcdr mys)))
             (set-mcdr! mys mxs)]
            [else (aux (mcdr mys))]))
    (aux mxs))
  (let ([mxs (list->mlist xs)])
    (begin (first-to-last! mxs)
           mxs)))

(define (mtake n mxs)
  (cond [(= n 0) null]
        [(null? mxs) null]
        [else (cons (mcar mxs) (mtake (- n 1) (mcdr mxs)))]))

(define (test)
  (mtake 15 (cycle '(0 1 2 3))))
```

```racket=
    (define (mreverse! xs)
        (if (not (pair? (mcdr xs)))
                xs
            (let ([tmp (mcar xs)])
            (set-mcar! xs (mcdr xs))
            (set-mcdr! xs tmp)
            )
        )
    )
    
    (define (m! xs)
        (let ([tmp (mcar xs)])
            (set-mcar! xs (mcdr xs))
            (set-mcdr! xs tmp)))
    
    (mreverse! (mcons 1 (mcons 2 (mcons 3 4))))
    ->
    (mcons 1 (mcons 2 (mcons 3 4)))
    
    tmp
    
    (1 (2 (3 4)))
  
    ((2 (3 4)) 1)
    
    (((3 4) 2) 1)
    (((4 3) 2) 1)
    
    
    
    
    (let (( x (mcons 1 (mcons 2(mcons 3 ( mcons 4 5))))))
  (mreverse! x)
  x)
  
  
    
(let (( x (mcons 1 2)))
  (m! x)
  x)

```

```=
(let (( x (mcons 1 (mcons 2 3))))
  (m! x)
  x)



(mcons (mcons 2 3) 1)

(define (mreverse! xs)
  (if(number? (mcdr xs))
      xs
      (m! (mcdr xs))
      (set-mcdr mcar))))
    
    
    
    
    
(define (m! xs)
(let ([tmp (mcar xs)])
    (set-mcar! xs (mcdr xs))
    (set-mcdr! xs tmp)
    xs))





(let (( x (mcons 1 (m! (mcons 2 3)))))
  (m! x))
  
  (mcons (mcons 3 2) 1)
  
  
 
  
  
  (let (( x (mcons 1 (mcons 2 (mcons 3 4)))))
  (set-mcdr! x (m! (mcdr x)))
  (m! x))
  
  
  
 (let (( x (mcons 1 (mcons 2 (mcons 3 4)))))
  (set-mcdr! (mcdr x) (m! (mcdr (mcdr x))))
   (set-mcdr! x (m! (mcdr x)))
  (m! x))
  
  
  (define (mreverse! xs) 
   (if (number? (mcdr x))
      (m! x)
      ((set-mcdr! (mcdr x) (m! ))
      (m! x)))
      )
      
      
        
  (define (mreverse! xs) 
   (if (number? (mcdr x))
      (m! x)
      (set-mcdr (m! (mcdr x)))
      (m! (mreverse! x))))
      
      
      
(define (m! xs)
(let ([tmp (mcar xs)])
    (set-mcar! xs (mcdr xs))
    (set-mcdr! xs tmp)
    xs))
      
(define (mreverse! xs) 
   (if (number? (mcdr xs))
      (m! xs)
      ((set-mcdr! (mcdr xs) (m!  (mcdr xs)) )
      (m! (mreverse! xs)))))    
      
      (mreverse! (mcons (mcons (mcons 4 3) 2) 1))
 ```     
      
      
# Podejscie 2

```racket=


(define (mreverse! xs)
  (if (or (empty? xs) (empty? (mcdr xs)))
      xs
      ((set-mcdr! xs (mreverse! (mcdr xs)))
      (set-mcdr! (mcdr xs) (mcar xs)))))

(let ((x (mcons 1 (mcons 2 (mcons 3 4)))))
  (mreverse! x)
  x)


```
    
  ```  
    (define (mreverse! xs)
  (if (not (pair? xs))
      xs
      ((set-mcdr! xs (mreverse! (mcdr xs)))
      (set-mcdr! (mcdr xs) (mcar xs)))))

(let ((x (mcons 1 (mcons 2 (mcons 3 4)))))
  (mreverse! x)
  x)



```

```
(define (mreverse! xs)
  (if (or (not (mpair? xs)) (not (mpair? (mcdr xs))))
      xs
      ((set-mcdr! xs (mreverse! (mcdr xs)))
      (set-mcdr! (mcdr xs) (mcar xs)))))
```