YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← 03-32 N Ary Function

dummy description

Get Embed Code
1 Language

Showing Revision 1 created 04/29/2012 by Amara Bot.

  1. What I want to do is define a function, and let's call it n_ary,
  2. and it takes (f), which should be a binary function, that is a function that takes
  3. exactly 2 arguments,
  4. and n_ary should return a new function that can take any number of arguments.
  5. We'll call this one f2, so that f2 of (a, b, c) is = f(a, f(b, c)),
  6. and that will be true for any number of arguments--2 or more.
  7. It doesn't have to just be a, b, c.
  8. So let's see if you can write this function n_ary.
  9. Here's a description of what it should do.
  10. It takes a binary function (f) as input, and it should return this n_ary function,
  11. that when given more than 2 arguments returns this composition of arguments.
  12. When given 2 arguments, it should return exactly what (f) returns.
  13. We should also allow it to take a single argument and return just that argument.
  14. That makes sense for a lot of functions (f), say for sequence.
  15. The sequence of 1 item is the item.
  16. For alt, the alternative of 1 item is the item.
  17. I mentioned addition and multiplication makes sense to say the addition of a number
  18. by itself is that number or same with multiplication.
  19. So that's a nice extension for n_ary.
  20. See if you can put your code here.
  21. So what we're doing is, we're passed in a function.
  22. We're defining this new n_ary function, putting the code in there,
  23. and then we're returning that n_ary function as the value of that call.