Posts

Showing posts with the label Structure and Interpretation of Computer Programs

SICP in Clojure: Chapter 2 (part 1)

Going on with my study of SICP, this is the first part of chapter 2 (that is a very big chapter and also has a lot of great examples). I also updated the code in the first chapter for some issues with chapter 2 names that were replicated (I made some private definitions there to use refer all here). The painter example of this book was really cool and actually you can create a painter with Quil library if you want to see it works. (ns sicp.chapter-2 (:require [sicp.chapter-1 :refer :all])) ;Common functions (defn null? [l] (and (seq? l) (empty? l))) ;Chapther 2: Building Abstractions with Data (defn linear-combination [a b x y] (+ (* a x) (* b y))) (declare add mul) (defn linear-combination [a b x y] (add (mul a x) (mul b y))) ;Chapter 2.1.1 (declare make-rat numer denom) (defn add-rat [x y] (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) (defn sub-rat [x y] (make-rat (- (* (numer x) (denom y)) ...

SICP in Clojure: Chapter 1

With a new year some new objectives should be set. Learn French Understand better functional programming Use Clojure in a real project (if you need help in some real project let me know :) ) This post is about the 2nd point. To learn more about functional programming I'm reading  Structure and Interpretation of Computer Programs .  This is really a great book but all the examples are in Lisp. To make it more actual I'm rewriting all the examples in clojure (and some exercises too).    I think it can be useful to others too, so here it is chapter 1: (ns sicp.chapter-1) ;1.1 Expressions (+ 137 349) (- 1000 334) (* 5 99) (/ 10 5) (+ 2.7 10) (+ 21 35 12 7) (* 25 4 12) (+ (* 3 5) (- 10 6)) (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) ;1.2 Naming and the Environment (def size 2) size (* 5 size) (def pi 3.14159) (def radius 10) (* pi (* radius radius)) (def circumference (* 2 pi radius)) c...