Computer Science

Functional Programming Languages

Functional programming languages are a type of programming language that emphasizes the use of functions to solve problems. They are designed to avoid side effects and mutable data, instead using immutable data structures and pure functions. Examples of functional programming languages include Haskell, Lisp, and ML.

Written by Perlego with AI-assistance

12 Key excerpts on "Functional Programming Languages"

  • Scala and Spark for Big Data Analytics
    • Md. Rezaul Karim, Sridhar Alla(Authors)
    • 2017(Publication Date)
    • Packt Publishing
      (Publisher)
    This is not a new concept but the Lambda Calculus, which provides the basis of FP, was first introduced in the 1930s. However, in the realm of programming language, the term functional programming refers to a new style of declarative programming paradigm that means programming can be done with the help of control, declarations, or expressions instead of classical statements commonly used in an old programming language, such as C.
    Passage contains an image

    Advantages of functional programming

    There are some exciting and cool features in FP paradigms such as composition, pipelining, and higher order functions that help to avoid writing unfunctional code. Alternatively, at least later on, this helps translate a unfunctional program into a functional style towards an imperative one. Finally, now let's see how we can define the term functional programming from the computer science perspective. Functional programming is a common computer science concept in which computations and the building structure of the program are treated as if you are evaluating mathematical functions that support immutable data and avoid state change. In functional programming, each function has the same mapping or output for the same input argument values.
    With the need for a complex software comes the need for good structured programs and software that are not difficult to write and are debuggable. We also need to write extendable code that will save us programming costs in the future and can contribute to easy writing and debugging of the code; even more modular software that is easy to extend and requires less programming efforts. Due to the latter contribution of functional programming, modularity, functional programming is considered as a great advantage for software development.
    In functional programming, there is a basic building block in its structure called functions without side effects (or at least very few) in most of your code. Without side effects, the order of evaluation really doesn't matter. When it comes to programming languages views, there are methods to force a particular order. In some FP languages (for example, eager languages such as Scheme), which have no evaluation order on arguments, you could nest these expressions in their own lambda forms as follows:
  • Functional Programming in C++
    • Ivan Cukic(Author)
    • 2018(Publication Date)
    • Manning
      (Publisher)
    is functional programming (FP)? This question is difficult to answer because no widely accepted definition exists. There’s a saying that if you ask two functional programmers what FP is, you’ll get (at least) three different answers. People tend to define FP through related concepts including pure functions, lazy evaluation, pattern matching, and such. And usually, they list the features of their favorite language.
    In order not to alienate anyone, we’ll start with an overly mathematical definition from the functional programming Usenet group:
    Functional programming is a style of programming that emphasizes the evaluation of expressions, rather than execution of commands. The expressions in these languages are formed by using functions to combine basic values. A functional language is a language that supports and encourages programming in a functional style.
    —FAQ for comp.lang.functional
    Over the course of this book, we’ll cover various concepts related to FP. I’ll leave it up to you to pick your favorites that you consider essential for a language to be called functional .
    Broadly speaking, FP is a style of programming in which the main program building blocks are functions as opposed to objects and procedures. A program written in the functional style doesn’t specify the commands that should be performed to achieve the result, but rather defines what the result is.
    Consider a small example: calculating the sum of a list of numbers. In the imperative world, you implement this by iterating over the list and adding the numbers to the accumulator variable. You explain the step-by-step process of how to sum a list of numbers. On the other hand, in the functional style, you need to define only what a sum of a list of numbers is. The computer knows what to do when it’s required to calculate a sum. One way you can do this is to say that the sum of a list of numbers equals the first element of the list added to the sum of the rest of the list, and that the sum is zero if the list is empty. You define what the sum is without explaining how to calculate it.
  • Functional Programming For Dummies
    • John Paul Mueller(Author)
    • 2019(Publication Date)
    • For Dummies
      (Publisher)
    (The next section, “Discovering Languages that Support Functional Programming,” describes the available languages in more detail.) In fact, you may even be performing functional programming tasks in your current language without realizing it. Every time you create and use a lambda function, you’re likely using functional programming techniques (in an impure way, at least). In addition to using lambda functions, languages that implement the functional programming paradigm have some other features in common. Here is a quick overview of these features: First-class and higher-order functions: First-class and higher-order functions both allow you to provide a function as an input, as you would when using a higher-order function in calculus. Pure functions: A pure function has no side effects. When working with a pure function, you can Remove the function if no other functions rely on its output Obtain the same results every time you call the function with a given set of inputs Reverse the order of calls to different functions without any change to application functionality Process the function calls in parallel without any consequence Evaluate the function calls in any order, assuming that the entire language doesn’t allow side effects Recursion: Functional language implementations rely on recursion to implement looping. In general, recursion works differently in functional languages because no change in application state occurs. Referential transparency: The value of a variable (a bit of a misnomer because you can’t change the value) never changes in a functional language implementation because functional languages lack an assignment operator. You often find a number of other considerations for performing tasks in functional programming language implementations, but these issues aren’t consistent across languages. For example, some languages use strict (eager) evaluation, while other languages use non-strict (lazy) evaluation
  • Hands-On Data Structures and Algorithms with Kotlin
    eBook - ePub

    Hands-On Data Structures and Algorithms with Kotlin

    Level up your programming skills by understanding how Kotlin's data structure works

    • Chandra Sekhar Nayak, Rivu Chakraborty(Authors)
    • 2019(Publication Date)
    • Packt Publishing
      (Publisher)

    Introduction to Functional Programming

    In this chapter, you will learn about another programming paradigm, called functional programming (FP ), and the data structures associated with it.
    Up until this chapter, you've learned about various data structures and collections frameworks. This book is about data structures and algorithms, so why a chapter on functional programming? The reason is quite simple; although Kotlin is not a pure functional language, it provides out-of-the-box support for functional programming, and functional programming requires a number of new data structures. We want you to be familiar with them, as well.
    In this chapter, we will cover the following topics:
    • Introducing functional programming
    • Immutability
    • Exploring pure functions and their side effects
    • Lambda and higher-order function s
    • Functional data structures
    • Introducing category theory
    Passage contains an image

    Introducing functional programming

    Functional programming is a programming paradigm, just like object-oriented programming (OOP ) or procedural programming. The definition of functional programming says, functional programming is a programming system that relies on structuring the program as the evaluation of mathematical functions with immutable data, and it avoids state-change .
    Just as OOP requires you to model your program like real-life objects (such as trees, birds, and so on), functional programming requires you to distribute your program into small pieces of code, making it modular and non-complex. Just like OOP, functional programming encourages code reusability and abstraction. In many functional programming experts' opinions, functional programming is a more efficient evolution of OOP. So, it's better to consider functional programming not as contradictory to OOP, but as a more efficient version of OOP.
    So, can we implement functional programming in any programming language? The answer is both yes and no. Just as with OOP, functional programming requires a few interfaces and support from the language; however, you can still follow the basic theories of functional programming in any language.
  • The JavaScript Workshop
    eBook - ePub

    The JavaScript Workshop

    A New, Interactive Approach to Learning JavaScript

    • Joseph Labrecque, Jahred Love, Daniel Rosenbaum, Nick Turner, Gaurav Mehla, Alonzo L. Hosford, Florian Sloot, Philip Kirkbride(Authors)
    • 2019(Publication Date)
    • Packt Publishing
      (Publisher)

    14. Understanding Functional Programming

    Overview
    By the end of this chapter, you will be able to use functional programming concepts such as pure functions, immutability, composition, and currying; use higher-order functions such as filter, map, and reduce; apply techniques such as cloning objects to reduce side effects in your code; and demonstrate strategies for reducing imperative logic and for loops in your code.

    Introduction

    In the previous chapter, we talked about how JavaScript is a multi-paradigm programming language. It's possible to write code with procedural, object-oriented, and functional design patterns. In this chapter, we'll look closely at the functional programming design pattern.
    Functional programming is a programming paradigm that has become popular in the last few years, though most JavaScript developers were unfamiliar with it before then.
    JavaScript is not a purely functional language like some others, such as Haskell, Scheme, and Clojure. However, JavaScript has support for functional structures and techniques if you choose to use them. It is worthwhile becoming familiar with its concepts and gaining a working knowledge of how to use them.
    Functional programming has a set of features. Among others, here are some of the important ones:
    • Pure functions
    • Immutability and avoiding shared state, mutable data, and side effects
    • Declarative rather than imperative
    • Higher-order functions
    • Function composition and piping
    • Currying functions
    • Reduces the use of traditional flow control structures such as for , while , and even if
    These concepts will be covered over the course of this chapter. If implemented correctly, functional programming can result in code that is more predictable, less error-prone, and easier to test compared to other programming methods.

    Pure Functions

    Pure functions are one of the pillars of functional programming. A function is pure if it always returns the same result when it's given the same parameters. It also cannot depend on or modify variables or state outside of the function's scope.
  • Mastering JavaScript Functional Programming
    eBook - ePub

    Mastering JavaScript Functional Programming

    Write clean, robust, and maintainable web and server code using functional JavaScript, 2nd Edition

    Becoming Functional - Several Questions

    Functional programming (or FP ) has been around since the earliest days of computing, and is going through a sort of revival because of its increased use with several frameworks and libraries, most particularly in JavaScript  ( JS ) . In t his chapter, we shall do the following:
    • Introduce some concepts of FP to give a small taste of what it means.
    • Show the benefits (and problems) implied by the usage of FP and why we should use it.
    • Start thinking about why JavaScript can be considered an appropriate language for FP.
    • Go over the language features and tools that you should be aware of in order to fully take advantage of everything in this book.
    By the end of this chapter, you'll have the basic tools that we'll be using in the rest of the book, so let's get started by learning about functional programming.
    Passage contains an image

    What is functional programming?

    If you go back in computer history, you'll find that the second oldest programming language still in use, Lisp, is based on FP. Since then, there have been many more functional languages, and FP has been applied more widely. But even so, if you ask people what FP is, you'll probably get two widely dissimilar answers.
    For trivia or history buffs, the oldest language still in use is Fortran, which appeared in 1957, a year before Lisp. Quite shortly after Lisp came another long-lived  language, COBOL, for business-oriented  programming.
    Depending on whom you ask, you'll either learn that it's a modern, advanced, enlightened approach to programming that leaves every other paradigm behind or that it's mainly a theoretical thing, with more complications than benefits, practically impossible to implement in the real world. And, as usual, the real answer is not in the extremes, but somewhere in between. Let's start by looking at the theory versus practice and see how we plan to use FP. 
    Passage contains an image

    Theory versus practice

    In this book, we won't be going about FP in a theoretical way. Instead, our point is to show you how some of its techniques and tenets can be successfully applied for common, everyday JavaScript programming. But—and this is important—we won't be going about this in a dogmatic fashion, but in a very practical way. We won't dismiss useful JavaScript constructs simply because they don't happen to fulfill the academic expectations of FP. Similarly, we won't avoid practical JavaScript features just to fit the FP paradigm. In fact, we could almost say that we'll be doing Sorta Functional Programming (SFP ) because our code will be a mixture of FP features, more classical imperative ones, and object-oriented programming (OOP
  • Get Programming with Haskell
    • Will Kurt(Author)
    • 2018(Publication Date)
    • Manning
      (Publisher)
    But a computer built with von Neumann architecture isn’t the only way to perform computation. Humans perform a wide variety of computations that have nothing to do with thinking of memory allocation and instruction sets: sorting books on a shelf, solving a derivative of a function in calculus, giving directions to friends, and so forth. When we write C code, we’re programming to a specific implementation of computation. John Backus, who led the team that created Fortran, asked in his Turing Award lecture, “Can programming be liberated from the von Neumann style?”
    This question leads to the second way to understand programming, which is the subject of the first unit in this book. Functional programming attempts to liberate programming from the von Neumann style. The foundations of functional programming are abstract, mathematical notions of computation that transcend a specific implementation. This leads to a method of programming that often solves problems simply by describing them. By focusing on computation, not computers, functional programming allows the programmer access to powerful abstractions that can make many challenging problems much easier to solve.
    The price of this is that getting started can be much more difficult. Ideas in functional programming are often abstract, and we must start by building the idea of programming up from first principles. Many concepts need to be learned before we can build useful programs. When working through this first unit, remember that you’re learning to program in a way that transcends programming a computer.
    Just as C is the nearly perfect embodiment of the von Neumann style of programming, Haskell is the purest functional programming language you can learn. As a language, Haskell commits fully to Backus’s dream and doesn’t allow you to stray back to more-familiar styles of programming. This makes learning Haskell more difficult than many other languages, but learning Haskell makes it impossible for you to not gain deep insights into functional programming as you go. By the end of this unit, you’ll have a strong enough foundation in functional programming to understand the basics of all other Functional Programming Languages, as well as being prepared for your journey to learn Haskell.
  • .NET Core 2.0 By Example
    • Rishabh Verma, Neha Shrivastava(Authors)
    • 2018(Publication Date)
    • Packt Publishing
      (Publisher)
    Functional programming is language-agnostic, which means it is not language-specific. Functional programming focuses on a structured approach; it doesn't have multiple entry and exit points. It doesn't have goto statements, so it is easy to create small modules and create large modules using small blocks of structured code (or in other words sub-modules), which increases the re-usability of code. One function can be used as the input of another function and that function can output a new function.
    Here are some rules which make it easy for us to understand functional programming:
    • In functional programming, a function's output never gets affected by outside code changes and the function always gives the same result for the same parameters. This gives us confidence in the function's behavior that it will give the expected result in all the scenarios, and this is helpful for multithread or parallel programming.
    • In functional programming, variables are immutable, which means we cannot modify a variable once it is initialized, so it is easy to determine the value of a variable at any given point at program runtime.
    • Functional programming works on referential transparency, which means it doesn't use assignment statements in a function. For example, if a function is assigning a new value to a variable such as shown here:
    Public int sum(x){x = x + 20 ;return x;} This is changing the value of x, but if we write it as shown here: Public int sum(x){return x + 20 ;} This is not changing the variable value and the function returns the same result.
    • Functional programming uses recursion for looping. A recursive function calls itself and runs till the condition is satisfied.
    Passage contains an image

    Functional programming features

    Let's discuss some functional programming features:
    • Higher-order functions
    • Purity
    • Recursion
    • Currying
    • Closure
    • Function composition
    Passage contains an image

    Higher-order functions (HOF)

    One function can take an input argument as another function and it can return a function. This originated from calculus and is widely used in functional programming. Order can be determined by domain and range of order such as order 0 has no function data and order 1 has a domain and range of order 0, if order is higher than 1, it is called a higher-order function. For example, the ComplexCalc function takes another function as input and returns a different function as output:
  • Functional Programming in JavaScript
    eBook - ePub

    Functional Programming in JavaScript

    How to improve your JavaScript programs using functional techniques

    • Luis Atencio(Author)
    • 2016(Publication Date)
    • Manning
      (Publisher)
    chapter 2 .
    Writing functional JavaScript code addresses most of these concerns. Using a set of proven techniques and practices based on pure functions, you can write code that is easy to reason about in the face of increasing complexity. Writing JavaScript functionally is a two-for-one deal, because you not only improve the quality of your entire application, but also gain more proficiency in and a better understanding of the JavaScript language.
    Because functional programming isn’t a framework or a tool, but a way of writing code, thinking functionally is radically different from thinking in object-oriented terms. But how do you become functional? How do you begin to think functionally? Functional programming is intuitive once you’ve grasped its essence. Unlearning old habits is the hardest part and can be a huge paradigm shift for most people who come from an object-oriented background. Before you can learn to think functionally, first you must learn what FP is.

    1.2. What is functional programming?

    In simple terms, functional programming is a software development style that places a major emphasis on the use of functions. You might say, “Well, I already use functions on a day-to-day basis at work; what’s the difference?” As I mentioned earlier, FP requires you to think a bit differently about how to approach the tasks you’re facing. It’s not a matter of just applying functions to come up with a result; the goal, rather, is to abstract control flows and operations on data with functions in order to avoid side effects and reduce mutation of state in your application. I know this sounds like a mouthful, but I’ll visit each of these terms further and build on them throughout the book.
    Normally, FP books start with computing Fibonacci numbers, but I’d rather start with a simple JavaScript program that displays text on an HTML page. What better text to print than the good ol’ “Hello World”:
  • F# Deep Dives
    eBook - ePub
    • Phillip Trelford, Tomas Petricek(Authors)
    • 2014(Publication Date)
    • Manning
      (Publisher)
    As a result of increasing complexity, your company might not be able to implement a desired financial model, AI behavior, or data analytical component, because it’s too complex. As a result, you won’t be able to provide the data that users and customers need (or not at the required quality), or an entire project or product may fail. In other words, without the right tools, you’ll often have to settle for a suboptimal solution.
    The business problems and implications outlined here are by no means complete, and they overlap. Handling efficiency or complexity often impacts time to market—you need to spend more time optimizing your system or tracking bugs. Efficiency and scalability are also often linked to correctness. In an attempt to make code more efficient, you could easily introduce bugs when trying to parallelize code that uses shared state.
    The key takeaway from this section is that developing software is hard. Exactly where the difficulties lie will depend on the particular software you’re developing. Understanding these difficulties and how they affect the business is crucial to finding the right way to tackle them, and one solution may be using a more appropriate programming language!

    Inferring business needs

    Many of the business problems discussed in the previous section are directly addressed by language features in functional-first programming languages or by their inherent aspects. We’ll discuss F#-specific features, but many of these observations apply to other functional-first languages.
    Functional-first programming languages
    We use the term functional-first to distinguish between purely functional languages and those that combine functional aspects with other paradigms. As with any language classification, this is, to a large extent, a subjective measure.
    In traditional functional languages, such as Miranda, Haskell, and ML, the only way to write programs is to fully adopt a functional style. There may be some exceptions (such as effects in ML), but the overall program structure has to be functional.
    In functional-first languages
  • Alan Turing
    eBook - ePub
    • S. Barry Cooper, J. van Leeuwen(Authors)
    • 2013(Publication Date)
    • Elsevier Science
      (Publisher)
    uniqueness type system warrants safe usage that avoids situations like (1) above. As the state is available, one can split it into different components, like files, the keyboard and whatever one needs. These can be modified separately, as long as they are not duplicated. Explicit access to the state allows to write the actors within Clean itself.
    We see that in both Haskell and Clean, dealing with I/O comes at a certain price. But this is well worth the advantages of pure Functional Programming Languages: having arbitrarily high meaningful information density, with modules that can be combined easily in a safe way.

    5 Current research

    5.1 Parallelism
    Pure functional languages are better equipped for programming multi-cores than imperative languages, as the result of a function is independent of its evaluation order. Therefore, modules in a functional program can be safely evaluated in parallel. As it costs overhead to send data between processors, one should restrict parallel evaluation to those functions having time consuming computations. Research on parallel evaluation of functional programs, see Hammond and Michaelson (1999) , has been revived by the advent of new multi-core machines (Marlow et al, 2009 ).
    5.2 Certification
    In languages like AGDA (Bove et al., 2009 ), or Coq Development Team, 2010 , one can fully specify a program and prove correctness. This demands even more skills from the programmer than pure functional languages already done: programming becomes proving. But the ideal of ‘formal methods’ (fully specifying software together with a proof of correctness) has become feasible. For example, Leroy (2009) gives a full certification of an optimised compiler for the kernel of the (imperative) language C.

    6 History and perspective

    The first functional language was Lisp (McCarthy et al., 1962 ). There is no type system, and I/O is done imperatively. By contrast, the language ML (Milner et al., 1997 ) and its modern variant F# (Syme et al., 2007 ) are impure as well, but strongly typed. Miranda (Turner, 1985 ) was one of the first pure Functional Programming Languages, with lazy evaluation and type inference. Clean (Plasmeijer and van Eekelen, 2002 ) and Haskell (Jones, 2003 ) are modern variants of Miranda. Haskell has become the de facto
  • React: Building Modern Web Applications
    • Jonathan Hayward, Artemij Fedosejev, Narayan Prusty, Adam Horton, Ryan Vice, Ethan Holmes, Tom Bray(Authors)
    • 2016(Publication Date)
    • Packt Publishing
      (Publisher)
    Chapter 5. Learning Functional Programming – The Basics JavaScript is a multiparadigm language that is not perfect for any paradigm it touches, but it has interesting features for its main paradigms. It is an object-oriented language, although the definition of object-oriented varies between object-oriented languages. It has been suggested that its prototypal inheritance may be less significant for object-oriented programming than a demonstration of how to create class-free objects instead of fumbling at the difficult task of getting the taxonomy right from the beginning. The definition of object-oriented also varies between multiparadigm languages with object-oriented features. For example, Python dynamically allows members to be added to existing objects, while Java requires members to be defined in a class. The object-oriented characteristics of JavaScript are useful and interesting, but especially in the past few years, they have been a source of frustration to programmers of other object-oriented languages, who have been told that JavaScript is object-oriented without sufficient information on how JavaScript is object oriented through an approach that differs profoundly from other major languages. Likewise, for functional programming, JavaScript has functional programming support, or at least some of it. But like JavaScript as a whole, functional JavaScript is not 100 percent in line with the The Good Parts. One common, although not universal, feature of Functional Programming Languages is tail call optimization, which says that recursive functions that only recur at the end are internally converted into a more commonplace style of loop that is faster and can go very deep without exhausting its quota of call stack space
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.