Created
November 3, 2015 08:27
-
-
Save nihilismus/9685aa6ad37331c39ae5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns funciones.recursivas) | |
;; Fuentes: | |
;; http://www.cs.us.es/~jalonso/cursos/i1m-15/temas/tema-6.html | |
;; http://www.glc.us.es/~jalonso/vestigium/i1m2015-ejercicios-de-definiciones-por-recursion/ | |
;; Recursión numérica | |
(defn factorial01 | |
[n] | |
(if (= n 0) | |
1 | |
(*' n (factorial01 (- n 1))))) | |
(defn factorial02 | |
[n] | |
(letfn [(factorial | |
[i t] | |
(if (> i n) | |
t | |
(factorial (inc i) (* i t))))] | |
(factorial 1 1))) | |
(defn productomn01 | |
[m n] | |
(if (= n 0) | |
0 | |
(+ m (producto01 m (- n 1))))) | |
(defn productomn02 | |
[m n] | |
(letfn [(producto | |
[i t] | |
(if (= i m) | |
t | |
(producto (inc i) (+ n t))))] | |
(if (> m 0) | |
(producto 1 n)))) | |
(defn potencia01 | |
[m n] | |
(if (= n 0) | |
1 | |
(* m (potencia01 m (dec n))))) | |
(defn mcd01 | |
[a b] | |
(if (= b 0) | |
a | |
(mcd01 b (rem a b)))) | |
;; Recursión sobre listas | |
(defn productons01 | |
[ns] | |
(if (= (count ns) 0) | |
1 | |
(* (first ns) (producto03 (rest ns))))) | |
(defn productons02 | |
[ns] | |
(if (nil? (first ns)) | |
1 | |
(* (first ns) (producto04 (rest ns))))) | |
(defn productons03 | |
[ns] | |
(letfn [(producto | |
[i t] | |
(if (>= i (count ns)) | |
t | |
(producto (inc i) (* t (ns i)))))] | |
(producto 0 1))) | |
(defn length01 | |
[xs] | |
(if (= (count xs) 0) | |
0 | |
(+ 1 (length01 (rest xs))))) | |
(defn length02 | |
[xs] | |
(if (nil? (first xs)) | |
0 | |
(+ 1 (length02 (rest xs))))) | |
(defn length03 | |
[xs] | |
(letfn [(lengthinterno | |
[i] | |
(if (>= i (count xs)) | |
i | |
(lengthinterno (inc i))))] | |
(lengthinterno 0))) | |
(defn reverse01 | |
[xs] | |
(if (= (count xs) 0) | |
[] | |
(conj (reverse01 (rest xs)) (first xs)))) | |
(defn reverse02 | |
[xs] | |
(if (nil? xs) | |
[] | |
(conj (reverse01 (rest xs)) (first xs)))) | |
(defn reverse03 | |
[xs] | |
(letfn [(reverseinterno | |
[i t] | |
(if (< i 0) | |
t | |
(reverseinterno (dec i) (conj t (xs i)))))] | |
(reverseinterno (- (count xs) 1) []))) | |
(defn concat01 | |
[xs ys] | |
(if (nil? (first ys)) | |
xs | |
(concat01 (conj xs (first ys)) (rest ys)))) | |
(defn concat02 | |
[xs ys] | |
(letfn [(concatinterno | |
[i t] | |
(if (= i (count ys)) | |
t | |
(concatinterno (inc i) (assoc t (+ (count xs) i) (ys i)))))] | |
(concatinterno 0 xs))) | |
(defn pertenece01 | |
[x xs] | |
(if (= (count xs) 0) | |
false | |
(or | |
(= x (first xs)) | |
(pertenece01 x (rest xs))))) | |
;; Recursión mutua | |
(defn par01 | |
[n] | |
(if (= n 0) | |
true | |
(impar01 (dec n)))) | |
(defn impar01 | |
[n] | |
(if (= n 0) | |
false | |
(par01 (dec n)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment