background preloader

With C++

Facebook Twitter

C++ - Functional-Style Programming in C++ C++ is a multiparadigm, systems-level language that provides high-level abstractions with very low (often zero) runtime cost. The paradigms commonly associated with C++ include procedural, object-oriented and generic programming. Because C++ provides excellent tools for high-level programming, even functional-style programming is quite reasonable. By functional-style programming, I don’t mean the programming is strictly functional, just that it’s easy to use many of the functional building blocks in C++. This article will focus on one of the most important functional programming constructs: working with values as opposed to identities.

Values vs. Let me first explain what I mean by working with values rather than identities. Unfortunately, it’s easy to confuse values and value types. The code in Figure 1 demonstrates a simple use of a value type. Figure 1 Using a Value Type Figure 2 Using a Reference Type Figure 3 The const Modifier C++ 11 Support for Functional-Style Programming. HaskC++ - Generic C++ programming from Haskell specification. What is it about? The type declaration of a static typed computer-program provides a kind of specification. Haskell, a functional programming language with an expressive type system is an appropriate candidate for prototype development of highly generic programs.

We use Haskell to design executable specifications of generic libraries which are implemented in C++ with concepts. The type-specification of the Haskell-program is translated into a type-specification of the C++ library. As this transformation follows a well-defined scheme, it is supported by a type compiler. How does it work? We provide a algorithmic method which transforms type-specification from a Haskell program into a system of C++ classes and concepts. Applications There are several applications for the HaskC++.

Downloads Functional_C++_Concepts libraryHaskell Type-Specification CompilerType-specification of the Origami libraryOrigami++ Publications D. Contact Daniel Lincke. Cap'n Proto: Cap'n Proto v0.2: Compiler rewritten Haskell -> C++ Kentonv on 12 Aug 2013 Today I am releasing version 0.2 of Cap’n Proto. The most notable change: the compiler / code generator, which was previously written in Haskell, has been rewritten in C++11. There are a few other changes as well, but before I talk about those, let me try to calm the angry mob that is not doubt reaching for their pitchforks as we speak. There are a few reasons for this change, some practical, some ideological. I’ll start with the practical. The practical: Supporting dynamic languages Say you are trying to implement Cap’n Proto in an interpreted language like Python.

What you really want to do is parse the schemas at start-up – the same time that the Python code itself is parsed. But with the schema parser written in C++, things become much simpler. The ideological: I’m an object-oriented programmer I really wanted to like Haskell. But when it comes down to it, I am an object-oriented programmer, and Haskell is not an object-oriented language.

Parser Combinators in C++ Bartosz Milewski's Programming Cafe | Concurrency, C++, Haskell. Monadic Parsing in C++ When researching parsing in Haskell, I always find this pdf: eprints.nottingham.ac.uk/223/1/pearl.pdf I will be referring back to this paper and encourage my readers to at least skim it as well. It may provide more understanding in how it works.

It describes a simple, yet hard to comprehend functional parser. I have attempted to translate this to C++, but first I wrote a more intuitive, less functional one. Just a simple calculator, somewhat close to what's described in the pdf, and it worked like this: The first stage took the input string and created a list of token type-lexeme pairs. These functions convert the vector of tokens to an expression tree that evaluates the arguments. "2*2" would parse to a tree with a multiplier at its root and two numbers as its leaves. This solution is theoretically consistent with building a language using flex and bison. Functional Parsing. The parser described in this paper does not act in the same way as my parser does. What does this class look like? Monads in C++ “You must be kidding!” Would be the expected reaction to “Monads in C++.”

Hence my surprise when I was invited to Boostcon 11 to give a three-hour presentation on said topic, a presentation which was met with totally unexpected level of interest. I imagine there was a small uptick in the sales of Haskell books afterwards. Before I answer the question: “Why are monads relevant in C++?” , let me first answer the question: “Why is Haskell relevant in C++?” Now, not everybody falls in love with the Haskell syntax (at least not at first sight) but it fits the functional paradigm much better than anything else. Armed with Haskell I could study and analyze some of the most sophisticated C++ metacode. Boost Proto is a library for implementing domain-specific languages (DSLs).

What Is an EDSL? Great things come out of abusing C++. In this post I will construct a simple EDSL in C++. My moment of Zen was when I realized that an EDSL corresponds to a Haskell reader monad. Let’s start with a short: What Does Haskell Have to Do with C++? If you want to understand C++ template metaprogramming (TMP) you have to know functional programming. Seriously. I want you to think of TMP as maximally obfuscated (subset of) Haskell, and I’ll illustrate this point by point. If you don’t know Haskell, don’t worry, I’ll explain the syntax as I go. The nice thing about single-paradigm languages like Haskell is that they have very simple syntax (think of Lisp that does everything with just a bunch of parentheses). I will start the Haskell-C++TMP mapping with basics, like functions and recursion, but I’ll try to cover a lot more, including higher-order functions, pattern matching, list comprehension (did you know it was expressible in C++?)

, and more. Keep in mind that my Haskell examples are runtime functions operating on runtime data whereas their C++ TMP equivalents are compile-time templates operating mostly on types. Functional Approach to Functions As a warm-up, let’s see how Haskell implements a simple function, the factorial: fact 4.