Apply the function to each of the elements of the list in order, and collect the results in a new list.” So What it says is this: “Here is a function, and here is a list of arguments. What about the This is a map, and is the alternative to iteration in LISP. You find you can’t live without them, and when you use a language which doesn’t allow them, you really miss ’em…. Once you start thinking about programming with them in mind, it really does help streamline your code. Why might you want to do this? One reason is it makes your code less cluttered if you need to define a relatively simple function and use it just once. But with the “#” as the argument to the function, this syntax allows you to avoid naming the function altogether. It’s like defining f(#) to be # * #, and then invoking f. This is usually called a pure function, and is essentially a function with no name. You might think that there’s no way the code could be shrunk that much, but it really is possible. In any case, I’m using this approach to illustrate a point. Of course you could embed the square function in the loop, but for more complicated functions, this is not usually done. Here’s a simple snippet of code which performs this task in Python: Rather than try to explain by using a definition (you can always google it), I’d like to show how I’d perform a simple calculation, like adding the sum of the squares of the first n integers. This week I’ll focus on what are typically referred to as aspects of functional programming. I’d like to continue that discussion today as a segue to talking about Mathematica, because the way I program in Mathematica is heavily influenced by my experience with LISP. The last installment of my coding autobiography was an introduction to LISP.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |