The long way through Software Craftsmanship

The language was prepared for that

Aug 10, 2015 - 2 minute read - Comments - clojurehaskelljavalanguage-comparisonprefix-notationoperatoroverloading

Many times I’ve written this function:

public boolean between(int lowerBound, int n, int upperBound){
	return lowerBound <= n &&
		n <= upperBound;
}

It may depend on the case, whether it is [], [), (] or (), to use mathematical terms.

When the two comparisons are the same ([] and ()), there is duplication in the comparisons.

Investigating a little bit on this in clojure, I’ve found this function:

<=

And its clojuredocs: Returns non-nil if nums are in monotonically non-decreasing order, otherwise false.

A sample usage:

(<= 1 2)
; true

(<= 1 2 1)
; false

The last part is the most interesting one. As this function is prepared to receive more than two parameters, it is very easy for the programmer to use it. We could say that the language was prepared for that.

The implementation:

(defn <=
  ([x] true)
  ([x y] (. clojure.lang.Numbers (lte x y)))
  ([x y & more]
   (if (<= x y)
     (if (next more)
       (recur y (first more) (next more))
       (<= y (first more)))
     false)))

Inspired by this, I’ve implemented the same function in haskell (for the repl):

let isBigger acc ele = (snd acc) && (fst acc) < ele in
   foldl (\acc ele -> (ele, isBigger acc ele)) (1, True) [1,2,1,3] 

and a simpler solution I’ve found on Stack Overflow:

isSorted :: (Ord a) => [a] -> Bool
isSorted xs = all (\(x, y) -> x <= y) $ zip xs (tail xs)

or

isSorted :: (Ord a) => [a] -> Bool
isSorted xs = and $ zipWith (<=) xs (tail xs)

Conclusion

Unless a more elegant, language-provided solution exists in haskell, the clojure one is way simpler. This is one of the benefits of prefix notation, that operators (e.g., +, -, *, <=) are overloaded to take more arguments than before.

Disclaimer about AI/GenAI

As of 2026-05-06, the text in these articles and blog entries has been written without AI/GenAI, except I sometimes use a spellchecker to fix errors. Think Word's spellchecker, not ChatGPT.

Notes, as of today (2026-05-06):

  • No code snippet has been automatically generated, nor vibe-coded, nor generated and reviewed.
  • I don’t have any article with AI contribution.

For future entries:

  • I may have used GenAI for the code in the repo. The code I exemplify/copy in the article will always be reviewed and tested, not vibe-coded. I will specify it in each snippet or at the top/bottom of the article.
  • I normally don't use it for the text contents, although if I have used it for the article text, it would be indicated as such.

Any entry before 2026-05-06 does not contain any AI/GenAI.

For more information, read the AI/GenAI Policy

Recognizing dependencies The Animal Laborans and the Homo Faber