background preloader

Functions

Facebook Twitter

First-class citizen. In programming language design, a first-class citizen (also object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities.

First-class citizen

These operations typically include being passed as a parameter, returned from a function, and assigned to a variable.[1] History[edit] The concept of first- and second- class objects was introduced by Christopher Strachey in the 1960s.[2][3] He did not actually define the term strictly, but contrasted real numbers and procedures in Algol: First and second class objects. In Algol, a real number may appear in an expression or be assigned to a variable, and either may appear as an actual parameter in a procedure call. During the 1990s, Raphael Finkel[5] proposed definitions of second and third class values, but these definitions have not been widely adopted.[6] Examples[edit] Few languages support continuations and GOTO-labels as objects at all, let alone as first-class objects.

First-class function. Concepts[edit] Higher-order functions: passing functions as arguments[edit] map :: (a -> b) -> [a] -> [b]map f [] = []map f (x:xs) = f x : map f xs void map(int (*f)(int), int x[], size_t n) { for (int i = 0; i < n; i++) x[i] = f(x[i]);} Anonymous and nested functions[edit] In languages supporting anonymous functions, we can pass such a function as an argument to a higher-order function: main = map (\x -> 3 * x + 1) [1, 2, 3, 4, 5] In a language which does not support anonymous functions, we have to bind it to a name instead: int f(int x) { return 3 * x + 1;} int main() { int list[] = {1, 2, 3, 4, 5}; map(f, list, 5);} Non-local variables and closures[edit] Once we have anonymous or nested functions, it becomes natural for them to refer to variables outside of their body (called non-local variables):

First-class function

Higher-order function. Takes one or more functions as an inputoutputs a function In the untyped lambda calculus, all functions are higher-order; in a typed lambda calculus, from which most functional programming languages are derived, higher-order functions are values with types of the form Example[edit] The following examples are not intended to compare and contrast programming languages, since each program performs a different task. def f(x): return x + 3 def twice(function, x): return function(function(x)) print(twice(f, 7)) f = (+3) twice function = function . function main = print (twice f 7)

Higher-order function

Clojure from the ground up: functions. We left off last chapter with a question: what are verbs, anyway?

Clojure from the ground up: functions

When you evaluate (type :mary-poppins), what really happens? User=> (type :mary-poppins)clojure.lang.Keyword To understand how type works, we’ll need several new ideas. First, we’ll expand on the notion of symbols as references to other values. Then we’ll learn about functions: Clojure’s verbs. Let bindings We know that symbols are names for things, and that when evaluated, Clojure replaces those symbols with their corresponding values. +, for instance, is a symbol which points to the verb #<core$_PLUS_ clojure.core$_PLUS_@12992c>. user=> +#<core$_PLUS_ clojure.core$_PLUS_@12992c> When you try to use a symbol which has no defined meaning, Clojure refuses: user=> cats CompilerException java.lang.RuntimeException: Unable to resolve symbol: cats in this context, compiling:(NO_SOURCE_PATH:0:0) But we can define a meaning for a symbol within a specific expression, using let. user=> (let [cats 5] (str "I have " cats " cats.

"))" Vars.