programming-beginners 2018-03-11

When would you use let vs an anonymous function?

could you write an example comparison?

in Scheme, let expands to an anonymous function; they're equivalent for all intents and purposes

let is just easier to read and write

and i guess maybe faster in Clojure

Thank you πŸ™‚ I was just reading about anonymous functions and couldn't figure out when it would be preferable to use them. But maybe I should have played with them a bit more first, seen if they really do react the same.

It's counter-intuitive, but not naming things is often the clearer and simpler approach. Names mean that the programmer trying to understand the code later has to meet all those people, so to speak. Also, naming things right is hard and every name is an opportunity to mis-name something, and therefore introduce confusion between the name and what it actually does. (Intermediate reading on this: https://en.wikipedia.org/wiki/Tacit_programming ) So most Clojurists find that if a function only gets used in one place, then it's better not to name it or its arguments (which means using the #(foo %) syntax for anonymous functions). Sometimes the middle ground has the most clarity: leave the function anonymous but name its arguments (which means using the (fn [bar] (...)) anonymous function syntax).

That’s really helpful, thank you!

1

given the choice...yes, never anonymous functions are useful for many more things than what let does

a typical example is...passing them to filter, remove, map...

you might find looking up Lambda Calculus interesting; it shows you that all that is needed for a programming language is a notion of anonymous functions, and a notion of function application. with just those two things, you can write programs that do anything (though naturally it would take a much longer time than in bigger languages with more stuff provided for you, like arithmetic and variables)

(it is the original mathematical system that lisps like Clojure are inspired by... as well as many other languages besides)

This is an area I really struggle with... I haven't done ANY maths since I was 16, and that was a long time ago. Calculus in general is something I can't imagine being able to get my head around just yet. Thank you for the video though - hopefully it will make it feel a little more accessible!

oh! don't worry, it's just called 'lambda calculus' - nothing to do with the other calculus (which i also struggle with) πŸ™‚

mathematicians love to use the same words for completely different things

That's helpful...!! πŸ˜‚ Thanks for the heads up, much appreciated.

😁 1