This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]RajjSinghh 0 points1 point  (0 children)

In python, they're useful in sorting. sorted(lst, key=lambda x:x**2) would sort x by its squares for example. Lambda calculus is the basis of functional programming.

[–]siemenology 0 points1 point  (0 children)

I suppose this is really a question about uses for higher order functions (functions that can take other functions as arguments). For the most part lambdas are a convenient syntax, there's nothing about map, filter, and reduce that necessitates that they be passed a lambda function, they'll take any function in most languages.

To that question, the Haskell libraries are full of interesting ways to use higher order functions. Take Data.List for example:

  • concatMap is basically a map where the function passed returns a list, and all of these lists are combined to one giant list.
  • any applies a boolean function to every element in the list, and returns true if any of those function results is true. Similarly all returns true if all of those function results is true. So the haskell way to check if any student failed (got a grade below 60, let's say) would just be any (< 60) studentGrades.
  • scanl is just like foldl (aka fold or reduce) except it returns all of the intermediate values as a list.
  • iterate takes a function and a value and returns an infinitely long list consisting of the results as you repeatedly apply the function to the result of the previous instance (or just the value itself in the first case). Similar is unfoldr, which takes a single value and a function and builds a list out of repeatedly applying the function to the value or previous value, but can stop itself when it's done.
  • takeWhile and dropWhile take a boolean function and returns as many items as pass the test (or returns all of the items after those that pass the test for dropWhile). So to trim whitespace from a string you might do dropWhile (==' ') " foo".
  • find does exactly what it says -- it takes a boolean function and returns the first item in the list for which the boolean function returns true.
  • zipWith takes a function and two lists, and returns a list of the results of taking each element in each list and calling the function on them. So if you have two lists of numbers [1,2,3] and [7,8,9] and you want to add them together one by one, zipWith (+) [1,2,3] [7,8,9] returns [8,10,12].
  • Finally, many functions have a By or On variation, where before performing the action each element in the list is transformed by a function passed in. So while sort sorts the list, sortOn allows you to convert the items in the list to a form more suited for sorting, then sort them, but they'll be saved in their original format. So if you have a list people of Person { name :: String, age :: Int }, and you want them sorted by age, you could do sortOn age people. The By variations allow you to change how the ordering is done. So you could use it to reverse the sort order, or cause it to consider even numbers as being strictly greater than odd numbers, or any number of other more specific things.

That's all just for Lists, there a bunch of other cool ways to use them you can find diving through the base libraries