Programación Funcional

EJEMPLOS

A continuación veremos algunos ejemplos en los diferentes lenguajes de programación funcional



HOLA MUNDO

Clojure

(defn mensaje [n]
n)
(println ( mensaje "hello-world"))
view raw main.clj hosted with ❤ by GitHub
Haskell

Scala

Lisp

Ocaml

Scheme

(define (mensaje n)
n)
(display (mensaje "Hello World"))
view raw main.scm hosted with ❤ by GitHub
Erlang

-module(main).
-import(io, [fwrite/1]).
-export([start/0]).
start() ->
fwrite("Hello, world!\n").
view raw main.erl hosted with ❤ by GitHub

FIBONACCI

Clojure

Haskell

Scala

Lisp

Ocaml

Scheme

(define (fibonacci n)
(if (< n 2)
n
(+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
(display "Fibonacci :")(display (fibonacci 10))
(newline)
view raw main.scm hosted with ❤ by GitHub
Erlang

JavaScript

Python

R


Ejemplo en Kotlin


Kotlin

Funciones puras

Clojure

(defn factorial [n]
(if (<= n 1)
1
(* n (factorial (- n 1)))))
(println (factorial 5))
view raw factorial.clj hosted with ❤ by GitHub
Haskell

module Main where
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)
main :: IO ()
main = do
let n = 5 -- Cambia el valor de n según tu necesidad
print ( factorial n)
view raw factorial.hs hosted with ❤ by GitHub
Scala

object Main {
def factorial (n : Integer): Integer = {
if (n <= 1) 1
else n * factorial(n - 1)
}
def main(args: Array[String]): Unit = {
println(factorial(5))
}
}
view raw factorial.scala hosted with ❤ by GitHub
Lisp

(defun factorial (n)
(if (<= n 1)
1
(* n (factorial (- n 1)))))
(print (factorial 5))
view raw factorial.lisp hosted with ❤ by GitHub
Ocaml

let rec factorial n =
if n <= 1 then 1
else n * factorial (n - 1);;
let () =
let n = 5 in
let result = factorial n in
print_endline (string_of_int result)
view raw factorial.ml hosted with ❤ by GitHub
Scheme

(define (factorial n)
(if (<= n 1)
1
(* n (factorial (- n 1)))))
(display (factorial 5))
(newline)
view raw factorial.scm hosted with ❤ by GitHub
Erlang

-module(main).
-import(io, [fwrite/1]).
-export([start/0]).
-export([factorial/1]).
factorial(0) -> 1;
factorial(N) when N > 0 -> N * factorial(N - 1).
start() ->
io:format("~w ", [factorial(5)]).
view raw factorial.erl hosted with ❤ by GitHub

Ruby

Funciones anónimas

Clojure

;;Anonymous function with fn
((fn [coll] (filter even? coll)) [1 2 3 4 5 6])
(println ((fn [coll] (filter even? coll)) [1 2 3 4 5 6]))
;;Anonymous function with #
(#(filter even? %) [1 2 3 4 5 6])
(println (#(filter even? %) [1 2 3 4 5 6]))
Haskell

module Main where
main = do
--Anonymous functions
let add = (\x y -> x + y)
let sub = (\x y -> x - y)
let prod = (\x y -> x * y)
putStrLn "The result of adding 3 and 4 is:"
print (add 3 4)
print (sub 3 4)
print (prod 3 4)
Scala

object Main {
def main(args: Array[String]): Unit = {
//Anonymous functions
var plus = (z:Int, y:Int)=> z+y
var sub = (z:Int, y:Int)=> z-y
var prod = (z:Int, y:Int)=> z*y
println(plus(10,12))
println(sub(10,12))
println(prod(10,12))
}
}
Lisp

;;Anonymous +
(print ((lambda (a b)
(+ a b))
410 90 ))
;;Anonymous -
(print ((lambda (a b)
(- a b))
410 90 ))
;;Anonymous *
(print ((lambda (a b)
(* a b))
410 90 ))
;;Anonymous (a^2 + b^2)/2
(print ((lambda (a b)
(/ (+ (* a a) (* b b)) 2))
410 90 ))
Ocaml

let plus = fun x y -> x+y
let sub = fun x y -> x-y
let prod = fun x y -> x*y
let sq = fun x y -> ((x * x + y * y) / 2);;
print_int (plus 10 45);
print_newline ();
print_int (sub 10 45);
print_newline ();
print_int (prod 10 45);
print_newline ();
print_int (sq 10 45);
Scheme

(lambda (a b)
(+ a b))
(lambda (a b)
(- a b))
(lambda (a b)
(* a b))
(lambda (a b)
(/ (+ (* a a) (* b b))2))
(display ((lambda (a b) (+ a b)) 14 7))
(newline)
(display ((lambda (a b) (- a b)) 14 7))
(newline)
(display ((lambda (a b) (* a b)) 14 7))
(newline)
(display ((lambda (a b) (+ (+ (* a a) (* b b)) 2)) 14 7))
(newline)
Erlang

-module(main).
-import(io, [fwrite/1]).
-export([start/0]).
square_list(List) ->
lists:map(fun(X) -> X * X end, List).
start() ->
Result = square_list([1, 2, 3, 4, 5]),
io:format("Squared list: ~p~n", [Result]).

Python


R

Funciones de orden superior


Ruby


Scala

Currying


Ruby




R


Scala

Procs


Ruby

Functor lista


Python



Scala

Functor función


Python

Caso de uso


Python

Effect tracking


Python

Operadores como funciones


R

Vectorización


R