17 Fix spelling, grammar, style, and a few semantic errors
source | link

You might want to read from the bottom up. sometimesSometimes I refer back to lower snippets, but never to higher ones, so it might help understanding.

readers whichReaders who do not know Haskell: am I clear? whenWhen am I not clear? I can't tell.

A foldable instance for our tree data structure (snippet 23). Foldable is a type class - as in, a class(/group) of types. theseThese are parallel to interfaces in Java. They essentially generalize over types, unifying types which have common characterstics, likecharacteristics; for example, they can be added together (Monoid), containers (Functor), can be printed as text (Show, which we have met already, in the show function), and so on. This one Unifiesunifies data types which are list-like, in that they can be iterated over, or can be flattened to a list.

In this snippet, we define the instance by defining foldr, which essentially iterates over the data type from right to left. nowNow, we can use a bunch of general pre-written code. firstFirst, we define a helper function to get a singletonesingleton tree, to avoid all the clutter.: s a = N E a E. nowNow:

This works by definintdefining a function sieve which filters the list and keeps only the numbers which doare not dividedivisible by any previous numberprime. It is defined recursively - to sieve is defined as to split the list to a first element p and a tail, filter from the tail any number divisible by p, sieve the remaining bit, attach p to the start of that, and return.

Again, we are working with infinite lists here - but the computation will halt in time as long as you won'tdon't require an infinite amount of primes to be computed.

In your first time reading this, you might think that the output of this quine would be missing the quotation marks, and why you would you once write putStr and once print? itIt sounds the same.

lengthLength 33 snippet:

The important thing to note is that we didn't use a loop. Haskell doesn't loop. Haskell recurses. Haskell doesn't have loops.it's farrther It's deeper than that: Haskell doesn't even have Control flow. How, you might ask? wellWell, it doesn't need any.

On with the details. This program prints an infinite increasing sequence of integers, starting from 0. go prints them starting with its input., then main calls it on 0.

do is a special syntactic power of Haskell. inIn this scenario, it just combines I/O actions, just like >> does (see snippet 22).

lengthLength 26 snippet:

This defines the map function, probably familiar to everyone, using foldr. noticeNotice that although we didn't declare map's type, the computer somehow knows its type is (a -> b) -> [a] -> [b], i.e. given a function from a to b, and a list of as, return a list of bs.

lengthLength 25 snippet:

The standard Hello World. noteNote the types: main has type of IO () and putStr has type of String -> IO () (a function from strings to I/O actions which return nothing).

lengthLength 23 snippet:

This is a standard definition of a Tree. How much easier fromthan all those lines required to define a tree in javaJava, C, or anything else.

let'sLet's break it down:

data - this declaration declares a data type. T a - a tree containing elements of type a. thisThis is the type we are defining. = - every value of T a will be any of the following, separated by a pipe |. E - one of the possible values of T s - the empty tree. N (T a) a (T a) - the other possible value of a tree - a node. eachEach node consists of the left child ((T a)) the element (a) and the right child ((T a)).

lengthLength 22 snippet:

lengthLength 18 snippet:

A better definition for fix. (seeSee snippet 14).) The problem with the first definition, fix f = f(fix f), is that every time we call fix f fix recalls fix f, which recalls fix f, generating endless copies of the same computation. This version fixes it by defining r (result) to be the result; as such, f r = r. soSo, let's define r = f r. nowNow we return r.

lengthLength 17 snippet:

lengthLength 16 snippet:

f is the doublemultiply-by-three function (also f = (*3))

lengthLength 15 snippet:

This defines the sum function using a fold. aA fold is basically a loop over the elements of a list with one accumelatoraccumulator.
foldl takes as arguments some function f and some initial value x for the accumulator and a list xs. theThe function f should get as input the previous accumulator value, and the current value of the list, and it returns the next accumulator.
thenThen the fold iterates on the list values, applying f on the previous accumulator, and then returns the last accumulator.

anotherAnother way to think about folds is like the fold 'inserts' f between the list values and with the initial accumulator in one of the sides. forFor example, foldl (*) 1 [4,2,5] evaluates to 1 * 4 * 2 * 5.

lengthLength 14 snippet:

The y combinator. It is usually named fix because it finds the fixpoint of the equasionequation f x = x. noteNote that x = infinite loop can also sometimes a solution, so fix (\x -> x^2 + 5*x + 7) won't solve the equation x^2 + 4*x + 7 = 0 but instead return an infinite loop.

You may also note that not always x = infinite loop is a solution, because of Haskel'sHaskell's laziness.

lengthLength 13 snippet:

This defines the function f that given a list returns the sum of it'sits squares. itIt is the function composition of the function sum and the functionmap(^2), which in turn is the function map applied to the function (^2) (the square function), which is in turn a section of the function ^ (sections were introduced at snippet 2, and composition at snippet 3).

As you can see, functions are quite important in a functional language like Haskell. inIn fact, it has been said that Haskell is the language with the most standard library functions which get functions as inputs or return functions as outputs (this is commonly known as a higher-order function.

lengthLength 10 snippet:

In fact, all data types ever can be defined in Haskell, when in other languages the number, references and array datatypesdata types need special support from the compiler. In practice there is special support in Haskell as it would be very inconvenient otherwise, but for example the Bool datatype is defined entirely in language.

lengthLength 9 snippet:

lengthLength 8 snippet:

This defines the function f which, given a non empty-empty list, will return its head.

Patterns in Haskell are like Python's sequence unpacking  , but generalized for all types. Patterns can either reject or match a value, and if it matches, it can bind variables to values.

langthLength 5 snippet:

This code snippet defined x, an infinite list made up entirely of 2. Usually in other languages x has to be evaluated before 2:x can ever be evaluated, but in Haskell we can do this.

lengthLength 4 snippet:

This snippet just encodes the singletonesingleton list [2]. : is the Cons operator in Haskell. In fact, the regular list syntax is just syntactic sugar for the cons operator and the empty list literal. thisThis tightly ties in into the way Haskell deals with Pattern matching and Data types (particularly the concept of constructor).

lengthLength 3 snippet:

lengthLength 2 snippet:

When this code is wrapped in parentheses (for syntactical reasons) it is called a "section". It is then a function that given some number, "fills up" the empty spot and returns one minus that number. thisThis notion is sometimes useful in a functional language like Haskell, where otherwise a lambda would be needed.

lengthLength 1 snippet:

You might want to read bottom up. sometimes I refer back to lower snippets, but never to higher ones, so it might help understanding.

readers which do not know Haskell: am I clear? when am I not clear? I can't tell.

A foldable instance for our tree data structure (snippet 23). Foldable is a type class - as in, a class(/group) of types. these are parallel to interfaces in Java. They essentially generalize over types, unifying types which have common characterstics, like, they can be added together (Monoid), containers (Functor), can be printed as text (Show, which we have met already, in the show function), and so on. This one Unifies data types which are list-like, in that they can be iterated over, or can be flattened to a list.

In this snippet, we define the instance by defining foldr, which essentially iterates over the data type from right to left. now, we can use a bunch of general pre-written code. first, define a helper function to get a singletone tree, to avoid all the clutter. s a = N E a E. now:

This works by definint a function sieve which filters the list and keeps only the numbers which do not divide by any previous number. It is defined recursively - to sieve is defined as to split the list to a first element p and a tail, filter from the tail any number divisible by p, sieve the remaining bit, attach p to the start of that, and return.

Again, we are working with infinite lists here - but the computation will halt in time as long as you won't require an infinite amount of primes computed.

In your first time reading this, you might think that the output of this quine would be missing the quotation marks, and why you would once write putStr and once print? it sounds the same.

length 33 snippet:

The important thing to note is that we didn't use a loop. Haskell doesn't loop. Haskell recurses. Haskell doesn't have loops.it's farrther than that: Haskell doesn't even have Control flow. How, you might ask? well, it doesn't need any.

On with the details. This program prints an infinite increasing sequence of integers, starting from 0. go prints them starting with its input. then main calls it on 0.

do is a special syntactic power of Haskell. in this scenario, it just combines I/O actions, just like >> does (see snippet 22).

length 26 snippet:

This defines the map function, probably familiar to everyone, using foldr. notice that although we didn't declare map's type, the computer somehow knows its type is (a -> b) -> [a] -> [b], i.e. given a function from a to b, and a list of as, return a list of bs.

length 25 snippet:

The standard Hello World. note the types: main has type of IO () and putStr has type of String -> IO () (a function from strings to I/O actions which return nothing).

length 23 snippet:

This is a standard definition of a Tree. How much easier from all those lines required to define a tree in java, C, or anything else.

let's break it down:

data - this declaration declares a data type. T a - a tree containing elements of type a. this is the type we are defining. = - every value of T a will be any of the following, separated by a pipe |. E - one of the possible values of T s - the empty tree. N (T a) a (T a) - the other possible value of a tree - a node. each node consists of the left child ((T a)) the element (a) and the right child ((T a)).

length 22 snippet:

length 18 snippet:

A better definition for fix. (see snippet 14). The problem with the first definition, fix f = f(fix f), is that every time we call fix f fix recalls fix f, which recalls fix f, generating endless copies of the same computation. This version fixes it by defining r (result) to be the result; as such, f r = r. so let's define r = f r. now we return r.

length 17 snippet:

length 16 snippet:

f is the double-by-three function (also f = (*3))

length 15 snippet:

This defines the sum function using a fold. a fold is basically a loop over the elements of a list with one accumelator.
foldl takes as arguments some function f and some initial value x for the accumulator and a list xs. the function f should get as input the previous accumulator value, the current value of the list, and returns the next accumulator.
then the fold iterates on the list values, applying f on the previous accumulator, and then returns the last accumulator.

another way to think about folds is like the fold 'inserts' f between the list values and with the initial accumulator in one of the sides. for example, foldl (*) 1 [4,2,5] evaluates to 1 * 4 * 2 * 5.

length 14 snippet:

The y combinator. It is usually named fix because it finds the fixpoint of the equasion f x = x. note that x = infinite loop can also sometimes a solution, so fix (\x -> x^2 + 5*x + 7) won't solve the equation x^2 + 4*x + 7 = 0 but instead return an infinite loop.

You may also note that not always x = infinite loop is a solution, because of Haskel's laziness.

length 13 snippet:

This defines the function f that given a list returns the sum of it's squares. it is the function composition of the function sum and the functionmap(^2), which in turn is the function map applied to the function (^2) (the square function), which is in turn a section of the function ^ (sections were introduced at snippet 2, and composition at snippet 3).

As you can see, functions are quite important in a functional language like Haskell. in fact, it has been said that Haskell is the language with the most standard library functions which get functions as inputs or return functions as outputs (this is commonly known as a higher-order function.

length 10 snippet:

In fact, all data types ever can be defined in Haskell, when in other languages the number, references and array datatypes need special support from the compiler. In practice there is special support in Haskell as it would be very inconvenient otherwise, but for example the Bool datatype is defined entirely in language.

length 9 snippet:

length 8 snippet:

This defines the function f which given a non empty list will return its head.

Patterns in Haskell are like Python's sequence unpacking  , but generalized for all types. Patterns can either reject or match a value, and if it matches, it can bind variables to values.

langth 5 snippet:

This code snippet defined x, an infinite list made up entirely of 2. Usually in other languages x has to be evaluated before 2:x can ever be evaluated, but in Haskell we can.

length 4 snippet:

This snippet just encodes the singletone list [2]. : is the Cons operator in Haskell. In fact, the regular list syntax is just syntactic sugar for the cons operator and the empty list literal. this tightly ties in into the way Haskell deals with Pattern matching and Data types (particularly the concept of constructor).

length 3 snippet:

length 2 snippet:

When this code is wrapped in parentheses (for syntactical reasons) it is called a "section". It is then a function that given some number, "fills up" the empty spot and returns one minus that number. this notion is sometimes useful in a functional language like Haskell, where otherwise a lambda would be needed.

length 1 snippet:

You might want to read from the bottom up. Sometimes I refer back to lower snippets, but never to higher ones, so it might help understanding.

Readers who do not know Haskell: am I clear? When am I not clear? I can't tell.

A foldable instance for our tree data structure (snippet 23). Foldable is a type class - as in, a class(/group) of types. These are parallel to interfaces in Java. They essentially generalize over types, unifying types which have common characteristics; for example, they can be added together (Monoid), containers (Functor), can be printed as text (Show, which we have met already, in the show function), and so on. This one unifies data types which are list-like in that they can be iterated over or flattened to a list.

In this snippet, we define the instance by defining foldr, which essentially iterates over the data type from right to left. Now, we can use a bunch of general pre-written code. First, we define a helper function to get a singleton tree, to avoid all the clutter: s a = N E a E. Now:

This works by defining a function sieve which filters the list and keeps only the numbers which are not divisible by any previous prime. It is defined recursively - to sieve is defined as to split the list to a first element p and a tail, filter from the tail any number divisible by p, sieve the remaining bit, attach p to the start of that, and return.

Again, we are working with infinite lists here - but the computation will halt in time as long as you don't require an infinite amount of primes to be computed.

In your first time reading this, you might think that the output of this quine would be missing the quotation marks, and why would you once write putStr and once print? It sounds the same.

Length 33 snippet:

The important thing to note is that we didn't use a loop. Haskell doesn't loop. Haskell recurses. Haskell doesn't have loops. It's deeper than that: Haskell doesn't even have Control flow. How, you might ask? Well, it doesn't need any.

On with the details. This program prints an infinite increasing sequence of integers, starting from 0. go prints them starting with its input, then main calls it on 0.

do is a special syntactic power of Haskell. In this scenario, it just combines I/O actions, just like >> does (see snippet 22).

Length 26 snippet:

This defines the map function, probably familiar to everyone, using foldr. Notice that although we didn't declare map's type, the computer somehow knows its type is (a -> b) -> [a] -> [b], i.e. given a function from a to b, and a list of as, return a list of bs.

Length 25 snippet:

The standard Hello World. Note the types: main has type of IO () and putStr has type of String -> IO () (a function from strings to I/O actions which return nothing).

Length 23 snippet:

This is a standard definition of a Tree. How much easier than all those lines required to define a tree in Java, C, or anything else.

Let's break it down:

data - this declaration declares a data type. T a - a tree containing elements of type a. This is the type we are defining. = - every value of T a will be any of the following, separated by a pipe |. E - one of the possible values of T s - the empty tree. N (T a) a (T a) - the other possible value of a tree - a node. Each node consists of the left child ((T a)) the element (a) and the right child ((T a)).

Length 22 snippet:

Length 18 snippet:

A better definition for fix. (See snippet 14.) The problem with the first definition, fix f = f(fix f), is that every time we call fix f fix recalls fix f, which recalls fix f, generating endless copies of the same computation. This version fixes it by defining r (result) to be the result; as such, f r = r. So, let's define r = f r. Now we return r.

Length 17 snippet:

Length 16 snippet:

f is the multiply-by-three function (also f = (*3))

Length 15 snippet:

This defines the sum function using a fold. A fold is basically a loop over the elements of a list with one accumulator.
foldl takes as arguments some function f and some initial value x for the accumulator and a list xs. The function f should get as input the previous accumulator value and the current value of the list, and it returns the next accumulator.
Then the fold iterates on the list values, applying f on the previous accumulator, and then returns the last accumulator.

Another way to think about folds is like the fold 'inserts' f between the list values and with the initial accumulator in one of the sides. For example, foldl (*) 1 [4,2,5] evaluates to 1 * 4 * 2 * 5.

Length 14 snippet:

The y combinator. It is usually named fix because it finds the fixpoint of the equation f x = x. Note that x = infinite loop can also sometimes a solution, so fix (\x -> x^2 + 5*x + 7) won't solve the equation x^2 + 4*x + 7 = 0 but instead return an infinite loop.

You may also note that not always x = infinite loop is a solution, because of Haskell's laziness.

Length 13 snippet:

This defines the function f that given a list returns the sum of its squares. It is the function composition of the function sum and the functionmap(^2), which in turn is the function map applied to the function (^2) (the square function), which is in turn a section of the function ^ (sections were introduced at snippet 2, and composition at snippet 3).

As you can see, functions are quite important in a functional language like Haskell. In fact, it has been said that Haskell is the language with the most standard library functions which get functions as inputs or return functions as outputs (this is commonly known as a higher-order function.

Length 10 snippet:

In fact, all data types ever can be defined in Haskell, when in other languages the number, references and array data types need special support from the compiler. In practice there is special support in Haskell as it would be very inconvenient otherwise, but for example the Bool datatype is defined entirely in language.

Length 9 snippet:

Length 8 snippet:

This defines the function f which, given a non-empty list, will return its head.

Patterns in Haskell are like Python's sequence unpacking, but generalized for all types. Patterns can either reject or match a value, and if it matches, can bind variables to values.

Length 5 snippet:

This code snippet defined x, an infinite list made up entirely of 2. Usually in other languages x has to be evaluated before 2:x can ever be evaluated, but in Haskell we can do this.

Length 4 snippet:

This snippet just encodes the singleton list [2]. : is the Cons operator in Haskell. In fact, the regular list syntax is just syntactic sugar for the cons operator and the empty list literal. This tightly ties in into the way Haskell deals with Pattern matching and Data types (particularly the concept of constructor).

Length 3 snippet:

Length 2 snippet:

When this code is wrapped in parentheses (for syntactical reasons) it is called a "section". It is then a function that given some number, "fills up" the empty spot and returns one minus that number. This notion is sometimes useful in a functional language like Haskell, where otherwise a lambda would be needed.

Length 1 snippet:

16 Small grammar fix
source | link

When this code is wrapped in parentheses (for syntactical reasons) it is called a "section". TtIt is then a function that given some number, "fills up" the empty spot and returns one minus that number. this notion is sometimes useful in a functional language like Haskell, where otherwise a lambda would be needed.

When this code is wrapped in parentheses (for syntactical reasons) it is called a "section". Tt is then a function that given some number, "fills up" the empty spot and returns one minus that number. this notion is sometimes useful in a functional language like Haskell, where otherwise a lambda would be needed.

When this code is wrapped in parentheses (for syntactical reasons) it is called a "section". It is then a function that given some number, "fills up" the empty spot and returns one minus that number. this notion is sometimes useful in a functional language like Haskell, where otherwise a lambda would be needed.

    Post Made Community Wiki by Dennis
15 added 2867 characters in body
source | link

Length 86 snippet

A foldable instance for our tree data structure (snippet 23). Foldable is a type class - as in, a class(/group) of types. these are parallel to interfaces in Java. They essentially generalize over types, unifying types which have common characterstics, like, they can be added together (Monoid), containers (Functor), can be printed as text (Show, which we have met already, in the show function), and so on. This one Unifies data types which are list-like, in that they can be iterated over, or can be flattened to a list.

In this snippet, we define the instance by defining foldr, which essentially iterates over the data type from right to left. now, we can use a bunch of general pre-written code. first, define a helper function to get a singletone tree, to avoid all the clutter. s a = N E a E. now:

sum (N (s 3) 7 (N E 5 (s 8))     === 23
product (N (s 3) 7 (N E 5 (s 8)) === 840
toList (N (s 3) 7 (N E 5 (s 8))  === [3,7,5,8]

and so on.

Here's a picture of our tree:

7
| \
3  5
    \
     8

Length 70 snippet

primes=sieve[2..] where
 sieve(p:xs)=p:sieve(filter(\x->x`mod`p/=0)xs)

This is a prime sieve!

(note: /= is what != is in other languages)

This works by definint a function sieve which filters the list and keeps only the numbers which do not divide by any previous number. It is defined recursively - to sieve is defined as to split the list to a first element p and a tail, filter from the tail any number divisible by p, sieve the remaining bit, attach p to the start of that, and return.

Again, we are working with infinite lists here - but the computation will halt in time as long as you won't require an infinite amount of primes computed.

take 4 primes === [2,3,5,7]

Length 68 snippet

Finally, a quine!

main=do putStr s;print s where s="main=do putStr s;print s where s="

In your first time reading this, you might think that the output of this quine would be missing the quotation marks, and why you would once write putStr and once print? it sounds the same.

In Haskell, putStr is a function that just prints the contents of the string it gets to stdout; print, though, prints things to stdout. So, print 4 is equivalent to putStr "4\n", but putStr 4 is nonsensical - 4 is not a string! So, when print gets a value, it first converts it into a string, and then prints that string. Generally the way to convert things to strings is to find the way you would write it down in code. So, the way you would write the string abc in a string in Haskell code is "abc", so print "abc" actually prints "abc", not abc.

How fortunate I have enough votes now, I won't have to golf these things

This snippet just encodes the singletone list [2]. : is the Cons operator in Haskell. In fact, the regular list syntax is just syntactic sugar for the cons operator and the empty list literal. this tightly ties in into the way Haskell deals with Pattern matching and Data types (particularly the concept of constructor).

: is the Cons operator in Haskell. In fact, the regular list syntax is just syntactic sugar for the cons operator and the empty list literal. this tightly ties in into the way Haskell deals with Pattern matching and Data types (particularly the concept of constructor).

Length 86 snippet

A foldable instance for our tree data structure (snippet 23). Foldable is a type class - as in, a class(/group) of types. these are parallel to interfaces in Java. They essentially generalize over types, unifying types which have common characterstics, like, they can be added together (Monoid), containers (Functor), can be printed as text (Show, which we have met already, in the show function), and so on. This one Unifies data types which are list-like, in that they can be iterated over, or can be flattened to a list.

In this snippet, we define the instance by defining foldr, which essentially iterates over the data type from right to left. now, we can use a bunch of general pre-written code. first, define a helper function to get a singletone tree, to avoid all the clutter. s a = N E a E. now:

sum (N (s 3) 7 (N E 5 (s 8))     === 23
product (N (s 3) 7 (N E 5 (s 8)) === 840
toList (N (s 3) 7 (N E 5 (s 8))  === [3,7,5,8]

and so on.

Here's a picture of our tree:

7
| \
3  5
    \
     8

Length 70 snippet

primes=sieve[2..] where
 sieve(p:xs)=p:sieve(filter(\x->x`mod`p/=0)xs)

This is a prime sieve!

(note: /= is what != is in other languages)

This works by definint a function sieve which filters the list and keeps only the numbers which do not divide by any previous number. It is defined recursively - to sieve is defined as to split the list to a first element p and a tail, filter from the tail any number divisible by p, sieve the remaining bit, attach p to the start of that, and return.

Again, we are working with infinite lists here - but the computation will halt in time as long as you won't require an infinite amount of primes computed.

take 4 primes === [2,3,5,7]

Length 68 snippet

Finally, a quine!

main=do putStr s;print s where s="main=do putStr s;print s where s="

In your first time reading this, you might think that the output of this quine would be missing the quotation marks, and why you would once write putStr and once print? it sounds the same.

In Haskell, putStr is a function that just prints the contents of the string it gets to stdout; print, though, prints things to stdout. So, print 4 is equivalent to putStr "4\n", but putStr 4 is nonsensical - 4 is not a string! So, when print gets a value, it first converts it into a string, and then prints that string. Generally the way to convert things to strings is to find the way you would write it down in code. So, the way you would write the string abc in a string in Haskell code is "abc", so print "abc" actually prints "abc", not abc.

How fortunate I have enough votes now, I won't have to golf these things

This snippet just encodes the singletone list [2]. : is the Cons operator in Haskell. In fact, the regular list syntax is just syntactic sugar for the cons operator and the empty list literal. this tightly ties in into the way Haskell deals with Pattern matching and Data types (particularly the concept of constructor).

14 such grammar
source | link
13 added 1436 characters in body
source | link
12 added 1436 characters in body
source | link
11 added 1436 characters in body
source | link
10 added 1783 characters in body
source | link
9 added 534 characters in body; added 113 characters in body; added 28 characters in body
source | link
8 added 445 characters in body
source | link
7 Formatting, small typos.
source | link
6 added 767 characters in body
source | link
5 added 639 characters in body
source | link
4 added 307 characters in body
source | link
3 added 246 characters in body
source | link
2 added 649 characters in body
source | link
1
source | link