;;;;;;;;;;;;;;;
; Self-test 6 ;
;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Production abstraction ;
; pair: ;
; object 0: year ;
; object 1: quantity in litres ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define create-production ; creates a pair with the year of production and the quantity produced in that year, in litres
(lambda (year litres)
(cons year litres)
)
)
(define year-production ; returns the year of production of a given production
(lambda (prod)
(car prod)
)
)
(define quantity-producao ; returns the quantity produced in a given production
(lambda (prod)
(cdr prod)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Parcel abstraction ;
; linked list: ;
; object 0: id ;
; object 1: pair (owner id, owner name) ;
; object 2: list( production1, production2, etc ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define create-parcel ; creates a parcel of land with an id code, a owner id code, a owner id name and no associated productions
(lambda (id oid oname)
(list id (cons oid oname) (list))
)
)
(define addd-prod-parcel ; adds a production object to the productions list of a parcel
(lambda (parcel production)
(append (list (car parcel) (cadr parcel)) (list (append (list production) (caddr parcel))))
)
)
(define prod-year-parcel ; returns the production for a given year uin a given parcel
(lambda (parcel year)
(letrec (
(aux
(lambda (production)
(cond
((null? production) 0)
((= (year-production (car production)) year) (quantity-production (car production)))
(else (aux (cdr production)))
)
)
)
)
(aux (caddr parcel))
)
)
)
(define avg-prod-parcel ; returns the average production for a given parcel, for all of its years
(lambda (parcel)
(letrec (
(aux
(lambda (production years sum)
(cond
((null? production)
(if (zero? years)
0
(/ sum years)
)
)
(else (aux (cdr production) (add1 years) (+ (quantity-production (car production)) sum)))
)
)
)
)
(aux (caddr parcel) 0 0)
)
)
)
(define year-max-prod-parcel ; returns the year for which the highest production was registred, for a given parcel
(lambda (parcel)
(letrec (
(aux
(lambda (production max year)
(cond
((null? production) year)
(else
(if (> (quantity-production (car production)) max)
(aux (cdr production) (quantity-production (car production)) (year-production (car production)))
(aux (cdr production) max year)
)
)
)
)
)
)
(aux (caddr parcel) 0 0)
)
)
)