Functional ProgrammingA programming paradigm which thinks of computing in terms of mathematical functions and it avoids state and mutable data. Imperative programming is based on the idea of state and mutable data and it corresponds to the kind of assembly language instructions which are understood by the CPU. Functional programming involved greater abstraction and code is written in a manner which looks more similar to the notation of functions in Mathematics textbooks. Here's the Wikipedia definition : In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in lambda calculus, a formal system developed in the 1930s to investigate function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus. First Order FunctionsThese are functions like Sin(x), Cos(x), tan(x), fibonacci(x); they all directly execute on "x". Higher Order FunctionsThese are functions of functions. For instance CubeOf(sin(x)), CubeOf(cos(x)), CubeOf(tan(x)). In this case, CubeOf is a function which operates on another function (sin or cos or tan). This, is a higher order function. We might also evaluate it this way : CubeOf(lambda,x) where lambda is a function which could be sin or cos or tan. RecursionThese are functions which call themselves. For example : Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2) // Starting conditions : Fibonacci(0) = 0, Fibonacci(1) = 1 CurryingInstead of making a function call to : CubeOf(sin,x) which then calls sin(x), CubeOf(cos,x) which then calls cos(x), CubeOf(tan,x) which then calls tan(x) It would be neater to call functions in this way: (function1 (function2)) (x) Where function1 = CubeOf, function2 = sin/cos/tan
|
Computer Science >