# Learning exercises: higher-order functions

I’m working on the Clojure track syllabus and want to cover the concepts of `map`, `filter` and `reduce` (aka `fold`), the basic trio of functional iteration.

I’m looking at the various functional language tracks and am not finding any to look at for inspiration or potential ports… I’d be happy to be corrected if I’ve overlooked them.

If this is indeed new territory, maybe we could use this thread to discuss possible ideas.

There is WIP content here (PR context). I also mention higher-order functions once in the WIP Approaches for Pangram.

Do you have specific questions about how to approach these concepts?

1 Like

No specific questions yet… but I covered them during my livestreams so I can distill the material from them (one of the exercises indeed was hamming). I’m thinking 3 separate learning exercises, first `map`, then `filter` and finally `reduce`.

There’s a couple of old issues:

In the third one, I mentioned that the elyses-transformative-enchantments from the JavaScript track would work well.

There’s also a port of the coordinate-transformation exercise that’s nearly ready, which teaches closures, which I think nicely introduces the concept of higher-order functions.

To begin, I might adapt this exercise from Advent of Code 2021: Day 1 - Advent of Code 2021

It has an extremely elegant solution which takes advantage of Clojure’s ability to map over multiple collections in parallel.

Would this extremely elegant solution be Clojure’s equivalent of

• Haskell: `length \$ filter id \$ zipWith (<) depths (tail depths)`
• Python: `sum(map(operator.lt, depths, depths[1:]))`

Yes, it appears so! Nice In my head I’ve been calling that pattern the anti-loop because the imperative solution is a nested `for` loop.

At the very latest, 10 years ago I learned about higher-order functions by reading this Learn You A Haskell chapter. (If it was earlier – probably not – it was not through that book.) I do not remember the process at all.

It was while reading that very chapter that it hit me that Clojure is basically Haskell with parentheses

You know… now that I’m looking at the Elyse’s Enchantments exercise again, it appears to be exactly these 3 concepts all in one exercise!

I can’t find this quickly right now: doth Clojure have algebraic data types/tagged unions and type classes?

Clojure will likely be my pick for the 12in23 Lisp month.

This is reassuring: apparently these functions aren’t as hard to understand as my impression of some introductory materials suggests.

Those concepts do exist in Clojure (kind of… I think), but they aren’t part of the main toolbelt. The philosophy is “just use maps”.

Perhaps List Ops as done in C, or Strain as done in Go might offer ideas for higher-order functions for Clojure?

1 Like

In the Common Lisp track I have concepts for mapping, filtering and reducing but not yet for functions that create other functions (which I think is the more interesting concept)

The concept exercises are just ways of introducing the student to the functions in the language. I can point to the specific exercises if wanted.

1 Like