‎Functional Programming on Perl‎

Christian Jaeger

LeafPair.com

 

Contents

Contents

Why Functional Programming?

Why Functional Programming?

"Difficult question.."

Why Functional Programming?

"Difficult question.."

Describing what it is, is not:

Why Functional Programming?

"Difficult question.."

Describing what it is, is not:

To program with functions that always return the same result for the same arguments (= pure functions).

fun square($x) { $x * $x }

Why Functional Programming? (2)

To program with functions that always return the same result for the same arguments (= pure functions).

Which implies not to use side channels

Why Functional Programming? (3)

So, the question is, what is the motivation for such a style?

"Difficult to answer."

Why Functional Programming? (3)

So, the question is, what is the motivation for such a style?

"Difficult to answer."

"It makes making proofs easier"

Why Functional Programming? (3)

So, the question is, what is the motivation for such a style?

"Difficult to answer."

"It makes making proofs easier"

Why Functional Programming? (3)

So, the question is, what is the motivation for such a style?

"Difficult to answer."

"It makes making proofs easier"

Why Functional Programming? (3)

So, the question is, what is the motivation for such a style?

"Difficult to answer."

"It makes making proofs easier"

Why Functional Programming? (3)

So, the question is, what is the motivation for such a style?

"Difficult to answer."

"It makes making proofs easier"

Goto considered harmful

Edsger Dijkstra, 1968 letter 'Go To Statement Considered Harmful' (originally 'A Case Against the Goto Statement')

sub Gsumofsquares {
    my ($from, $to)=@_;
    my $tot=0;
    my $i=$from;
  test:
    goto calculate if $i <= $to;
    return $tot;
  calculate:
    $tot+= $i * $i;
    $i++;
    goto test;
}


sub sumofsquares {
    my ($from, $to)=@_;
    my $tot=0;
    for (my $i= $from; $i<=$to; $i++) {
	$tot+= $i * $i;
    }
    $tot
}

Goto considered harmful (2)

The unbridled use of the go to statement has as an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress. ... The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program.

Goto considered harmful (2)

The unbridled use of the go to statement has as an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress. ... The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program.

goto, or structured programming constructs, are for control flow

Goto considered harmful (2)

The unbridled use of the go to statement has as an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress. ... The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program.

goto, or structured programming constructs, are for control flow

Goto considered harmful (2)

The unbridled use of the go to statement has as an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress. ... The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program.

goto, or structured programming constructs, are for control flow

Goto considered harmful (2)

The unbridled use of the go to statement has as an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress. ... The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program.

goto, or structured programming constructs, are for control flow

Goto considered harmful (2)

The unbridled use of the go to statement has as an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress. ... The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program.

goto, or structured programming constructs, are for control flow

"Globals are evil"

A Gradual Change

Programming with functions

is not difficult to explain

It may look unusual, though.

Classification of side effects

Impure functions can read or write side effects in several ways

Classification of side effects (2)

Notable: mutable data structures can be (unintended) side effect channels.

Side-effect free code and datastructures are complementing each other.

Thanks for listening!

Questions?

Get code and docs from functional-perl.org