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
;;1.45 Definir una macro MI-DO que tenga exactamente la misma funcionalidad que la macro DO, | |
;;pero que además de devolver el valor correspondiente cuando se cumpla la condición de finalización, | |
;;devuelva un segundo valor que indique el número de iteraciones que se han realizado. No se deben | |
;;utilizar las primitivas DO, DO*, DOLIST, DOTIMES. | |
(defmacro iterar (steps final &rest body) | |
`(labels ((main ,(mapcar #'car steps) | |
(if ,(car final) ,(cadr final) | |
(progn ,@body | |
(main ,@(apply #'append |
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
;; Ultimo commit con propuestas brillantes de http://twitter.com/_ogf_ | |
(defn score-single [ps] | |
(case ps 1 100 5 50 0)) | |
(defn score-triple [ps] | |
(case ps 1 1000 (* ps 100))) | |
(defn scores-by-freq [[ps freq]] | |
[(* (quot freq 3) | |
(score-triple ps)) |
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
(ns string-calc) | |
(defprotocol Adder | |
(add [self])) | |
(extend-type java.lang.String | |
Adder | |
(add [s] | |
(apply + (map #(Integer/parseInt %) | |
(.split s ","))))) |
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
;;1.48 No todos los sistemas de LISP implementan el mecanismo | |
;;Backquote. Definir BACKQUOTE de forma que tenga el mismo | |
;;efecto que Backquote y permita manejar de forma apropiada | |
;;expresiones como COMA y COMA-AT de la forma que se | |
;;muestra en el siguiente ejemplo: | |
;;> (BACKQUOTE (A B (LIST ‘C ‘D) (COMA (LIST ‘E ‘F) | |
;; (COMA-AT (LIST ‘G ‘H))) | |
;;(A B (LIST ‘C ‘D) (E F) G H) | |
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
(defn fizz-buzz [n] | |
(str (when (zero? (mod n 3)) "fizz") | |
(when (zero? (mod n 5)) "buzz"))) | |
(defn fizz-buzz [n] | |
(let [when-div-by | |
#(when (zero? (mod n %1)) %2) | |
join str] | |
(join (when-div-by 3 "fizz") | |
(when-div-by 5 "buzz")))) |
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
(ns roman-numerals | |
(:use clojure.test)) | |
(def digits ["I" "IV" "V" "IX" "X" "XL" "L" "XC" "C" "CD" "D" "CM" "M"]) | |
(def digits-ints (zipmap [1 4 5 9 10 40 50 90 100 400 500 900 1000] digits)) | |
(defn floor [nums n] | |
(first (filter #(<= % n) (reverse (sort nums))))) | |
(defn from-int [n] |
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
(defpackage game-of-life) | |
(defun get-neighbours (grid x y) | |
(let ((result 0) (xo (- x 1)) | |
(yo (- y 1))) | |
(dotimes (i 3) | |
(dotimes (j 3) | |
(let ((dx (+ xo i)) | |
(dy (+ yo j))) | |
(when (and (array-in-bounds-p grid dx dy) |
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
(defpackage 8-queens) | |
(defun copy-array (array) | |
(let ((dims (array-dimensions array))) | |
(adjust-array | |
(make-array dims :element-type | |
(array-element-type array) | |
:displaced-to array) | |
dims))) |
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
%% Los polinomios estan representados como lista de coeficientes | |
%% en la forma 1+2x+3x^2+4x^3 >> [1,2,3,4] | |
%% Suma de polinomios | |
%% (1+2x)+(3x+4x^2)=1+5x+4x^2 | |
%% suma([1,2],[0,3,4],S) >> S=[1,5,4] | |
suma([],[],[]). | |
suma([],YS,YS). | |
suma(XS,[],XS). | |
suma([XH|TX],[YH|TY],[S|TS]):- |
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
%% Carta mas alta=valor carta | |
%% Pareja=30 | |
%% Trio=60 | |
%% Doble pareja=90 | |
%% Full=120 | |
%% Color=150 | |
%% Poker=180 | |
%% Escalera color=210 | |
consecutivos([]). |