Last active
December 13, 2021 02:46
-
-
Save viglioni/2de15e54d10dfc9005384ce49fe1737c to your computer and use it in GitHub Desktop.
Código escrito durante o live coding "Introdução ao LISP"
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
;; | |
;; Link para o live coding: https://www.youtube.com/watch?v=IIp9YaXRHVY | |
;; | |
;; | |
;; Exemplo de map | |
;; | |
(map | |
(fn [x] (str "Hello " x)) | |
'("World" "There")) ;; => ("Hello World" "Hello There") | |
;; | |
;; Exemplo com let | |
;; | |
(defn func-let [x] | |
(let [y (+ x 1) | |
z (* y 2)] | |
(list x y z))) | |
(func-let 1) ;; => (1 2 4) | |
;; | |
;; Resolvendo um exercício de algoritmos | |
;; Entrada: uma lista L de números e um número N | |
;; Saída: uma lista com todos os pares de números de L | |
;; que quando multiplicados resultam no número N | |
;; | |
(def list-ex [1 2 3 4 5 6 7 8 9 10]) | |
(def n-ex 12) | |
(defn bruno-solver [l n] | |
(map vec | |
(let [hashmap (set l)] | |
(reduce | |
(fn [acc val] | |
(let [num (/ n val)] | |
(if (contains? hashmap num) | |
(conj acc (hash-set num val)) | |
acc))) | |
(set []) | |
l)))) | |
(bruno-solver list-ex n-ex) ;; => ([4 3] [2 6]) | |
;; | |
;; Fórmula de resolução da função quadrática | |
;; "Báscara" | |
;; | |
;; (-b +- sqrt(delta)) / 2a | |
(defn quadratic-solver [a b c] | |
(let [delta (- (* b b) (* 4 a c))] | |
[(/ (+ (- b) (Math/sqrt delta)) (* 2 a)) | |
(/ (- (- b) (Math/sqrt delta)) (* 2 a))])) | |
(quadratic-solver 1 -5 6) ;; => [3.0 2.0] | |
;; | |
;; Macros | |
;; | |
;; | |
;; Definir funções passando parênteses ao invés de colchetes | |
;; como é a "defun" em emacs-lisp | |
;; | |
(defmacro defun [name args & [body]] | |
`(defn ~name ~(vec args) | |
~body)) | |
(defun emacs-inc (x) (+ 1 x)) | |
(emacs-inc 10) ;; => 11 | |
;; | |
;; Definir uma macro que permite usar funções de maneira | |
;; infixadas, p.ex. (1 + 1) | |
;; | |
(defmacro bin-in [[operand-1 operator operand-2]] | |
`(~operator ~operand-1 ~operand-2)) | |
(bin-in (1 + 2)) ;; => 3 | |
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
;; | |
;; Link para o live coding: https://www.youtube.com/watch?v=IIp9YaXRHVY | |
;; | |
;; | |
;; Exemplo de map | |
;; | |
(seq-map | |
(lambda (x) (concat "Hello " x)) | |
'("World" "There")) ;; => ("Hello World" "Hello There") | |
;; | |
;; Exemplo com let | |
;; | |
(defun func-let (x) | |
(let* ((y (+ x 1)) | |
(z (* y 2))) | |
(list x y z))) | |
(func-let 1) ;; => (1 2 4) | |
;; | |
;; Macros | |
;; | |
;; | |
;; Definir funções passando colchetes ao invés de parênteses | |
;; como é a "defn" em clojure | |
;; | |
(defmacro defn (name args &rest body) | |
`(defun ,name ,(append args nil) | |
,@body)) | |
(defn clj-inc [x] (+ 1 x)) | |
(clj-inc 10) ;; => 11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment