---
# System prepended metadata

title: Untitled

---

```ruby=

;ZADANIE 19

#lang racket

(require racket/contract)

; (provide
;   (contract-out
;     [with-labels with-labels/c]
;     [foldr-map foldr-map/c]
;     [pair-from pair-from/c]))

; (provide with-labels/c
;          foldr-map/c
;          pair-from/c)

(define (with-labels f xs)
        (if (null? xs) 
        null
        (list (cons (cons (f (car xs))
                          (car xs))
                    null)
              (with-labels f (cdr xs)))))
  
(define with-labels/c
    (parametric->/c [a b] (-> (-> a b)
                              (listof a)
                              (listof (cons/c b (cons/c a null/c))))))

(define foldr-map/c 
    (parametric->/c [e a ve] (-> (-> e a (cons/c ve a))
                                 a 
                                 (listof e)
                                 (cons/c (listof ve) a))))
                                
     
     
(define (foldr-map f a xs)
    (define (it a xs ys)
        (if (null? xs)
            (cons ys a)
            (let [(p (f (car xs) a))]
                (it (cdr p) 
                    (cdr xs) 
                    (cons (car p) ys)))))
    (it a (reverse xs) null))

#ja mom takiego
(define/contract (foldr-map f acc xs)
  foldr-map/c
  (if (null? xs) (cons null acc)
      (let* ([next (foldr-map f acc (cdr xs))]
             [prod (f (car xs) (cdr next))])
        (cons (cons (car prod) (car next)) (cdr prod)))))

#(foldr-map (lambda(x a) (cons a (+ a x))) 0 '(1 2 3))

#### https://hackmd.io/@ppolesiuk/HkeuYvaY8
; make-list naturalnie zdefiniowalibyśmy tak
;(define (make-list es)
;  (if (null?)
;      (null-expr))
;      (cons-expr (car es) (make-list es)))
#### zajebisty ten foldr/l
; ale łatwo zauważyć, że jest to instancja funkcji foldr.
; W końcu consa checemy zamienić na cons-expr,
; a nulla na (null-expr).
(define (make-list es)
  (foldr cons-expr (null-expr) es))
####
(foldr f init xs)

(define (foldr-definition f init xs...)
  (if (null? xs)
      init
      (f (car xs) (foldr-definition xs)))
####

(define pair-from/c
    (parametric->/c [x fx gx] (-> (-> x fx)
                                  (-> x gx)
                                  (-> x (cons/c fx gx))))) 

(define/contract (pair-from f g) #tylko to wypierdoli sprawdzarkę :( 
  pair-from/c
  (lambda (x) 
    (cons (f x)
          (g x))))

((pair-from (lambda (x) (+ x 1))
            (lambda (x) (* x 2))) 2)
```
