F    LP

Functional Logic Progamming

Enieber Cunha

  • Developer JS and Android

  • Love in Functional Programming

  • Player Cello

FLP is fun

but... first

What is FLP?

Functional Programming

+

Logic Programming

Lets get start the end...

Logic Programming

Logic programming is a type of programming paradigm which is largely based on formal logic. Any program written in a logic programming language is a set of sentences in logical form, expressing facts and rules about some problem domain.. (Wikipidia)

https://en.wikipedia.org/wiki/Logic_programming

function isOlder(age) {
    if (age >= 18) {
        return true
    } else {
        return false
    }
}

Functional Programming

In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. (wikipedia)

https://en.wikipedia.org/wiki/Functional_programming

Why use FP?

Why i need LP?

Functional programming you need lees code


const animals = [ 
     { name: "Toto", species: "dog"},
     { name: "Bidu", species: "dog"},
     { name: "Mial", species: "cat"},
     { name: "Garfield", species: "cat"},
     { name: "Ze", species: "parrot"},
]


var isDog = function(animal) {
    return animal.species === "dog"
}

var isCat = function(animal) {
  return animal.species === 'cat'
}

var dogs = animals.filter(isDog)
var cats = animals.filter(isCat)

console.log(dogs)
console.log(cats)

Features FP

  • Higher-order Function

  • First-class Function

  • Pure functions

  • Recursion

const animals = [ 
     { name: "Toto", species: "dog"},
     { name: "Bidu", species: "dog"},
     { name: "Mial", species: "cat"},
     { name: "Garfield", species: "cat"},
     { name: "Ze", species: "parrot"},
]

var isDog = function(animal) {
    return animal.species === "dog"
}

isDog(animals[3])  //return is true

Higher-order Function

First-Class

const animals = [ 
     { name: "Toto", species: "dog"},
     { name: "Bidu", species: "dog"},
     { name: "Mial", species: "cat"},
     { name: "Garfield", species: "cat"},
     { name: "Ze", species: "parrot"},
]

// isDog is a function
// [].filter() is a function 

animals.filter(isDog)
 /*
    return [     
     { name: "Toto", species: "dog"},
     { name: "Bidu", species: "dog"}
    ]
*/
function sum(n1, n2) {
    return n1 + n2
}

sum(1,1) // 2

Pure Function

Recursion?

this is a challenge..

Write the fibonacci sequence using recursion

Thanks...

FLP

By Enieber Cunha

FLP

  • 821