Sunday, March 03, 2019

SICP in Clojure: Chapter 1, Exercise 4

Expressions are either primitive or compound. Procedures are either primitive or compound. A combination is an expression of the form (operator operands ...). This is my Clojure solution to Chapter 1, Exercise 4:

(ns sicp.ch1.ex04)

(defn a-plus-abs-b
  [a b]
  ((if (pos? b) + -) a b))

(a-plus-abs-b 5 -10) ; => 15

(a-plus-abs-b 5 10) ; => 15

No comments: