Created
March 23, 2012 15:00
-
-
Save fbanados/2171473 to your computer and use it in GitHub Desktop.
Auxiliar 2 CC4101 - Ejercicios 1, 2, 3, 4, 5a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang plai | |
(print-only-errors #t) | |
;fibs : int -> int | |
;Retorna el n-esimo numero de fibonacci | |
(define (fibs n) | |
(cond | |
((equal? n 1) 0) | |
((equal? n 2) 1) | |
(else (+ (fibs (- n 1)) (fibs (- n 2))))) | |
) | |
(test (fibs 4) 2) | |
(test (fibs 5) 3) | |
;member? : Any (Listof Any) -> bool | |
(define (member? element list) | |
(cond | |
((empty? list) #f) | |
((equal? element (car list)) #t) | |
(else (member? element (cdr list))) | |
)) | |
(test (member? 1 '(1 2 3)) #t) | |
(test (member? 1 '(2 3)) #f) | |
(define invert (λ (l) (foldl cons '() l))) | |
;split : Any (Listof Any) -> ( (Listof Any) . (Listof Any)) | |
(define (split elemento lista) | |
(letrec | |
((aux (λ (elemento lista) | |
(cond | |
((empty? lista) '()) | |
((equal? elemento (car lista)) (list (cdr lista))) | |
(else (cons (car lista) (aux elemento (cdr lista)))) | |
)))) | |
(let ((temp (invert (aux elemento lista)))) | |
(cons (invert (cdr temp)) | |
(car temp))))) | |
(test (split 3 '(1 2 3 4 5)) '((1 2) 4 5)) | |
(test (split #f '(1 2 3 4 #f 'elemento)) '((1 2 3 4) 'elemento)) | |
;quicksort: (Listof Any) -> (Listof Any) | |
(define (quicksort lista) | |
(cond | |
((empty? lista) '()) | |
(else | |
(let* | |
((pivote (car (drop lista(floor (/ (length lista) 2))))) | |
(resto (append | |
(take lista (floor (/ (length lista) 2))) | |
(cdr (drop lista (floor (/ (length lista) 2)))) | |
)) | |
(menores (filter (λ (x) (< x pivote)) resto)) | |
(mayores (filter (λ (x) (>= x pivote)) resto))) | |
(append (quicksort menores) (list pivote) (quicksort mayores)))))) | |
(test (quicksort '(4 2 3 1 5)) '(1 2 3 4 5)) | |
;create-book : void -> (Listof pair) | |
(define (create-book) empty) | |
(test (create-book) '()) | |
;add-person: string string (Listof pair) -> (Listof pair) | |
(define (add-person name phone book) | |
(cons (cons name phone) book)) | |
(test (add-person "felipe" "888" (create-book)) | |
(list (cons "felipe" "888"))) | |
;in-book?: string (Listof pair) -> boolean | |
(define (in-book? name book) | |
(cond | |
((empty? book) #f) | |
((equal? (caar book) name) #t) | |
(else (in-book? name (cdr book))))) | |
(test (in-book? "felipe" (add-person "felipe" "888" (create-book))) #t) | |
(test (in-book? "jorge" (add-person "felipe" "888" (create-book))) #f) | |
;get-phone: string (Listof pair) -> string | |
(define (get-phone name book) | |
(cond | |
((empty? book) "") | |
((equal? (caar book) name) (cdar book)) | |
(else (get-phone name (cdr book))))) | |
(test (get-phone "felipe" (add-person "felipe" "888" (create-book))) "888") | |
(test (get-phone "jorge" (add-person "felipe" "888" (create-book))) "") | |
; <Person> ::= <String> <Number> | |
; <AddressBook> ::= <mtBook> | <Person> <AddressBook> | |
(define-type Person | |
(person (name string?) (phone number?))) | |
(define-type AddressBook | |
(mtBook) | |
(book (person Person?) (next AddressBook?))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nope, aun no entiendo Scheme