About This

If all goes well, this will become a little online Clojure tutorial for the Facebook Clojure Community.

If it doesn't go well, blame me - my name is Carl Smotricz and I'm the only (so far) author.

I'm no Clojure guru. I'm an enthusiast looking to share my enthusiasm with others. Comments, suggestions, criticism, contributions are welcome.

6. Maximum Success!

To maximize success, we need to escape the paradigm of the one-function program. This page will push our function count up to... 3!

Maximally basic

To get started...
Write a function max2 that will return the larger (more positive) of two numeric arguments (a, b)!
Hint:

  • This may be a bit awkward to do with the less-than function ( < ). Remember, there's also a greater-than ( > ) ! 

Testing

I couldn't get Nightcode's "Test" button to do anything useful. Here's how I set up for automated unit testing (which you'll later find is a Very Good Thing):
  1. Add "(:use clojure.test)" to the ns statement:

    This allows us to use the following functions deftest and is.
  2. Add a function like this:

    This defines a series of assertive tests. Assertions use the "is" function to affirm that the contained condition is true. If it isn't, the test suite will let you know your assumptions were wrong.
  3. Throw out the println for "hello world" in -main and replace it with run-tests, like so:


    Be sure to make the name for run-tests match the name of your namespace, which in turn derives from the name of your project! My project name was "maxine". Also note that that name needs to be quoted (just a single quote on the left - a quote doesn't need to be closed, as it ends where the enclosed construct does).
So then, when you Build and Run your program, it will run your tests for you, and you'll get a report something like this:


Absolute Maximum 

This puts a bit of a twist on things:
The absolute value of a number is its value if it's positive, or the negative of its value if it's negative. In other words, you want to end up with whatever number you started with, but without the sign, if any.

With this in mind,

Write a function absmax that will return the maximum of the absolute value of two numbers!
Hints:

  • Based on the material so far, you will need at least 3 if expressions. It's mildly complicated but very do-able if you concentrate on what you're doing. There are better ways to do this, but please don't cheat - we'll get there soon enough!
Here are some tests:

(deftest absmaxtest
  (is (= 5 (absmax 3 5)))
  (is (= -5 (absmax 3 -5)))
  (is (= -99 (absmax -99 0)))
)

Combining Functions

We've been combining ifs and (in)equality tests for a while now, but now it's time to combine functions of our own!

The duplication of code in absmax is terrible. To factor out the "absolute value" part,
Write a function absval to return the absolute value of its single argument v !
You can test in the REPL or write a test - as you prefer.

Now, as the crowning achievement for this page,

Write a better, simplified version of absmax (call it absmax2) that uses absval to calculate its absolute values.

No comments:

Post a Comment