Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;.
- ;;Autor: Carlos A Delgado
- ;;Fecha: 08 de Agosto de 2020
- ;;Contrato: buscar-mayor: lista de números -> numero
- ;;Propósito: Dada una lista de 5 números retornar el mayor
- ;;Ejemplos
- ;;(buscar-mayor (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 empty))))) ) 5
- ;;(buscar-mayor (cons 15 (cons 20 (cons 83 (cons 44 (cons 5 empty))))) ) 83
- (define (buscar-mayor lst)
- (cond
- [(not (list? lst)) (error "El argumento de entrada debe ser una lista")]
- [(and
- (> (first lst) (first (rest lst)))
- (> (first lst) (first (rest (rest lst))))
- (> (first lst) (first (rest (rest (rest lst)))))
- (> (first lst) (first (rest (rest (rest (rest lst)))))))
- (first lst)]
- [(and
- (> (first (rest lst)) (first (rest (rest lst))))
- (> (first (rest lst)) (first (rest (rest (rest lst)))))
- (> (first (rest lst)) (first (rest (rest (rest (rest lst)))))))
- (first (rest lst))]
- [(and
- (> (first (rest (rest lst))) (first (rest (rest (rest lst)))))
- (> (first (rest (rest lst))) (first (rest (rest (rest (rest lst)))))))
- (first (rest (rest lst)))]
- [(>
- (first (rest (rest (rest lst)))) (first (rest (rest (rest (rest lst))))))
- (first (rest (rest (rest lst))))]
- [else
- (first (rest (rest (rest (rest lst)))))]
- ))
- (check-expect (buscar-mayor (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 empty))))) ) 5)
- (check-expect (buscar-mayor (cons 15 (cons 20 (cons 83 (cons 44 (cons 5 empty))))) ) 83)
- ;;Autor: Carlos A Delgado
- ;;Fecha: 08 de Agosto de 2020
- ;;Contrato: buscar-simbolo: lista de simbolos, simbolo -> booleano
- ;;Propósito: Indicar si un símbolo está en una lista
- ;;Ejemplos
- ;;(buscar-simbolo (cons 'a (cons 'b (cons 'c (cons 'd (cons 'e empty))))) 'c) #T
- ;;(buscar-simbolo (cons 'a (cons 'b (cons 'c (cons 'd (cons 'e empty))))) 'f) #F
- (define (buscar-simbolo lst sym)
- (cond
- [(not (and (cons? lst) (symbol? sym))) (error "Debe ingresar una de lista de símbolos y un simbolo")]
- [(equal? sym (first lst)) #T]
- [(equal? sym (first (rest lst))) #T]
- [(equal? sym (first (rest (rest lst)))) #T]
- [(equal? sym (first (rest (rest (rest lst))))) #T]
- [(equal? sym (first (rest (rest (rest (rest lst)))))) #T]
- [else #F]
- ))
- (check-expect (buscar-simbolo (cons 'a (cons 'b (cons 'c (cons 'd (cons 'e empty))))) 'c) #T)
- (check-expect (buscar-simbolo (cons 'a (cons 'b (cons 'c (cons 'd (cons 'e empty))))) 'f) #F)
- ;;Recursión
- ;;Autor: Carlos A Delgado S
- ;;Fecha: 08 de Agosto de 2020
- ;;Contrato: factorial: numero -> numero
- ;;Propósito: Calcular n!
- ;;(fact 5) 120
- ;;(fact 8) 40320
- (define (fact n)
- (cond
- [(not (number? n)) (error "La entrada debe ser un número")]
- [(= n 0) 1] ;;Caso base o trivial
- [else (* n (fact (- n 1)))] ;;Caso recursivo
- ))
- (check-expect (fact 5) 120)
- (check-expect (fact 8) 40320)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement