• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/4

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

4 Cards in this Set

  • Front
  • Back

first-class function

By “first-class” we mean that functions can be computed, passed, stored, etc. wherever other values can be computed, passed, stored, etc. As examples, we can pass them to functions, return them from functions, put them in pairs, have them be part of the data a datatype constructor carries, etc. “Function closures” refers to functions that use variables defined outside of them, which makes first-class functions much more powerful, as we will see after starting with simpler first-class functions that do not use this ability.

higher-order function

The term higher-order function just refers to a function that takes or returns other functions.


Terms like first-class functions, function closures, and higher-order functions are often confused with each other or considered synonyms. Because so much of the world is not careful with these terms, we will not be too worried about them either. But the idea of first-class functions and the idea of function closures really are distinct concepts that we often use together to write elegant, reusable code. For that reason, we will delay the idea of closures, so we can introduce it as a separate concept.

generic types (also called parametic polymorphism)

Let us now consider the type of n_times, which is (’a -> ’a) * int * ’a -> ’a. It might be simpler at first to consider the type (int -> int) * int * int -> int, which is how n_times is used for x1 and x2 above: It takes 3 arguments, the first of which is itself a function that takes and returns an int. Similarly, for x3 we use n_times as though it has type (int list -> int list) * int * int list -> int list. But choosing either one of these types for n_times would make it less useful because only some of our example uses would type-check. The type (’a -> ’a) * int * ’a -> ’a says the third argument and result can be any type, but they have to be the same type, as does the argument and return type for the first argument. When types can be any type and do not have to be the same as other types, we use different letters (’b, ’c, etc.)

anonymous function

ML has a much more concise way to define functions right where you use them, as in this final, best version:


fun triple_n_times (n,x) = n_times((fn y => 3*y), n, x)


This code defines an anonymous function fn y => 3*y. It is a function that takes an argument y and has body 3*y. The fn is a keyword and => (not =) is also part of the syntax. We never gave the function a name (it is anonymous, see?), which is convenient because we did not need one. We just wanted to pass a function to n_times, and in the body of n_times, this function is bound tof.