Difference between revisions of "Fibs and Facts"

From The fun Wiki
Jump to navigation Jump to search
(Created page with "Fibonacci and Factorial are two of the basic examples for functional programming languages. Here we show their implementation in Wu. == Fibonacci == fib n = (> 2 n) 1 (+ 1...")
 
Line 7: Line 7:
 
== Factorial ==
 
== Factorial ==
 
   fact n = (> 2 n) 1 (* n (fact (- n 1)));
 
   fact n = (> 2 n) 1 (* n (fact (- n 1)));
 +
or,
 
   fact n = foldr + 0 (range 1 n);
 
   fact n = foldr + 0 (range 1 n);

Revision as of 12:22, 3 May 2022

Fibonacci and Factorial are two of the basic examples for functional programming languages. Here we show their implementation in Wu.

Fibonacci

 fib n = (> 2 n) 1 (+ 1 (+ (fib (- n 1) (- n 2))));
 linfib x y n = (== 0 n) y (linfib y (+ x y) (- n 1));

Factorial

 fact n = (> 2 n) 1 (* n (fact (- n 1)));

or,

 fact n = foldr + 0 (range 1 n);