background preloader

CS - General

Facebook Twitter

Basic CS stuff

Cheatsheet.codeslower.com/CheatSheet.pdf. Fundamentals. Algebraic data type. The values of a sum type are typically grouped into several classes, called variants. A value of a variant type is usually created with a quasi-functional entity called a constructor. Each variant has its own constructor, which takes a specified number of arguments with specified types. The set of all possible values of a sum type is the set-theoretical sum, i.e. the disjoint union, of the sets of all possible values of its variants. enumerated types are a special case of sum types in which the constructors take no arguments.

Values of algebraic types are analyzed with pattern matching, which identifies a value by its constructor or field names and extracts the data it contains. Examples[edit] One of the most common examples of an algebraic data type is the singly linked list. Data List a = Nil | Cons a (List a) Cons is an abbreviation of construct. Data Tree = Empty | Leaf Int | Node Tree Tree In most languages that support algebraic data types, it is possible to define parametric types.

Functional Programming

What is Functional Programming? - Christopher Bennage - Devlicio.us - Just. Disclaimer: I’m still pretty green with functional programming. This is me working out my own understanding. Wikipedia defines Functional Programming (FP) this way: “functional programming is a programming paradigm that treats computations as the evaluation of mathematical functions and avoids state and mutable data.” [reference] Let’s break this apart. Programming Paradigm What’s a “programming paradigm”? It seems to me that most popular languages allow for the use of more than one paradigm. Computations In this definition, I take the term very generically. Evaluations of Mathematical Functions This is the core concept behind functional languages. F(t) = -4.9t2 + 19.6t + 3 With this function, we pass in a value for t and we get back a value representing the height.

Avoids State and Mutable Data This point follows naturally from the last one, however it was very confusing to my object-oriented mind. Characteristics of Functional Languages I’ll begin by discussing the first three items. 27 Dec 2010 This article is also available in Japanese. I am about to break a promise. Almost three years ago, I promised myself that I would never write an article about monads. There are too many such articles already; so many, in fact, that people are often confused by the sheer proliferation.

Everyone seems to have a different take on the subject, meaning that those attempting to learn the concept for the first time are stuck trying to reason out the commonalities between burritos, space suits, elephants and desert Bedouins. I’m not going to add to this menagerie of confusing analogies. The fact is that none of these parallels are entirely accurate. Math (or not) Here’s the thing about monads which is hard to grasp: monads are a pattern, not a specific type. Just as a quick Ruby refresher, we can rewrite this code in the following way: Here’s the puzzler: what is the semicolon (;) doing? This function does exactly the same thing as the Ruby version. Still with me? The Monad Pattern. The Algebra of Data, and the Calculus of Mutation » Lab49 Blog.

With the spreading popularity of languages like F# and Haskell, many people are encountering the concept of an algebraic data type for the first time. When that term is produced without explanation, it almost invariably becomes a source of confusion. In what sense are data types algebraic? Is there a one-to-one correspondence between the structures of high-school algebra and the data types of Haskell? Could I create a polynomial data type? Do I have to remember the quadratic formula? Are the term-transformations of (say) differential calculus meaningful in the context of algebraic data types? Isn’t this all just a bunch of general abstract nonsense?

We’ll investigate these questions, and perhaps demystify this important concept of functional languages. Algebraic Expressions at the Level of Types To understand the concept of algebraic data types, simply stated, we need to unify the set of concepts that we associate with algebra, and the set of concepts that we associate with data types. x. C9 Lectures: Dr. Erik Meijer - Functional Programming Fundamentals, Chapter.