;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname svolgimento_lez_06) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;(define (mypow n m) ; (if (eq? m 0) 1 ; (* n (mypow n (- m 1))) ; ) ;) (define (amypow n m a) (if (eq? m 0) a (amypow n (- m 1) (* a n)) ) ) (define (mypow n m) (amypow n m 1) ) (define (mfirst l) (car l)) (define (msecond l) (cadr l)) ; prima versione even_odd_split: gestione completamente esplicita (define (even_odd_split l) (cond ( (null? l) (list '() '()) ) ( (even? (car l)) (list (cons (car l) (mfirst (even_odd_split (cdr l)))) (msecond (even_odd_split (cdr l))) ) ) ( else (list (mfirst (even_odd_split (cdr l))) (cons (car l) (msecond (even_odd_split (cdr l)))) ) ) ) ) ; seconda versione: utilizzo di let per semplificare codice e calcolo (define (even_odd_split2 l) (if (null? l) (list '() '()) (let ( [res (even_odd_split2 (cdr l))] [h (car l)] ) ( if (even? h) (list (cons h (mfirst res) ) (msecond res)) (list (mfirst res) (cons h (msecond res))) ) ) ) ) ; terza versione: utilizzo di filter (NB filter è già definita in scheme) (define (mfilter fn l) (cond ((null? l) '()) ((fn (car l)) (cons (car l) (mfilter fn (cdr l))) ) ( else (mfilter fn (cdr l))) ) ) (define (even_odd_split3 l) (list (mfilter even? l) (mfilter odd? l)) )