This chapter starts with a couple of “warm-up exercises” so that you can get comfortable with your ClojureScript development environment. First, a quick review of how to define functions. Here is the generic model for a function:
(defn function-name [parameters] function-body)
Here is a function that takes an acceleration and an amount of time as its parameters and returns the distance traveled:
(defn distance [accel time] (/ (* accel time time) 2.0)
You can also put a documentation string between the function name and parameter list:
(defn distance "Calculate distance traveled by an object moving with a given acceleration for a given amount of time." [accel time] (/ (* accel time time) 2.0)
Create a project named formulas (see “Creating a ClojureScript Project”) and start a browser REPL (read/evaluate/print/loop). If you haven’t yet installed ClojureScript, follow the instructions in Appendix B, and create a project to work with.
In the REPL, type the preceding
distance function and test it.
Defining functions in the REPL is fine for a quick test, but it is not something you want to do on an application-level scale. Instead, you want to define the functions in a source file. In the formulas project, open the src/formulas/core.cljs file and create functions for these formulas:
Here is some sample output.
(in-ns 'formulas.core) switches you to that namespace so that you can type the function name without having to specify the module that it is in. If you update the source,
(require 'formulas.core :reload) will recompile the code:
cljs.user=> (in-ns 'formulas.core) nil formulas.core=> (require 'formulas.core :reload) nil formulas.core=> (distance 9.8 5) 122.5 formulas.core=> (kinetic-energy 35 4) 280 formulas.core=> (centripetal 30 2) 450
See a suggested solution: “Solution 1-2”.
def special form lets you bind a symbol to a value. The symbol is globally available to all functions in the namespace where it is defined. Add a function named
gravitational-force that calculates the gravitational force between two masses whose centers of mass are at a distance r from each other to your code:
, where the gravitational constant
def for the gravitational constant.
Here is the calculation for two masses of 100 kg that are 5 m apart:
formulas.core=> (gravitational-force 100 100 5) 2.67136e-8
to rebind a symbol to a value with code like this:
However, this is somewhat frowned upon. Global, shared, mutable (changeable) variables can be problematic, as described in this answer to a question on StackExchange. You will find that ClojureScript’s functional programming model makes the need for such global variables much less frequent. As a beginning programmer, when you create a variable with
def, treat it as if it were an (unalterable) algebraic variable and do not change its value.
See a suggested solution: “Solution 1-3”
To create local bindings of symbols to values within a function, you use
let is followed by a vector of symbol and value pairs.1
In this étude, you will write a function named
monthly-payment that calculates monthly payments on a loan. Your function will take the amount of the loan, the annual percentage rate, and the number of years of the loan as its three parameters. Calculate the monthly payment according to this formula:
let to make local bindings for:
pow function with code in this format:
;; Thus, to calculate 3 to the fifth power:
You can also use this shorthand:
Here is some sample output for a loan of $1,000.00 at 5.5% for 15 years. You can also check the results of your function against the results of the
PMT function in your favorite spreadsheet:
See a suggested solution: “Solution 1-4”.
Here’s a somewhat more complicated formula―determining the amount of sunlight in a day, given the day of year and the latitude of your location.
Write a function named
daylight with two parameters: a latitude in degrees and a Julian day. The function returns the number of minutes of
"Convert degrees to radians"
(.-PI js/Math) gets the
The variable D holds the number of hours of daylight, so multiply that by 60 for your final result. If you feel that these formulas are a bit too complicated to type as single expressions (I certainly did!), break them down by using
let for the parts.
On Mac OSX or Linux, you can get a Julian date with the
$ date '+%j' # today 127 $ date -d '2015-09-15' '+%j' # arbitrary date 258
Your results should be very close to those generated by the National Oceanic and Atmospheric Administration spreadsheets, which use a far more complicated algorithm than the one given here.
See a suggested solution: “Solution 1-5”.
let is followed by a vector of binding forms and values. Binding forms include destructuring as well as simple symbols.