cubbi.com: fibonacci numbers in hope languages: [english] [русский]
Hope
Date: 1978
Type: Pure functional programming language
Usage: academic

ALGORITHM 1A: BINARY RECURSION
uses lists;
! Function f returns nth Fibonacci number
! It uses ALGORITHM 1A: BINARY RECURSION
dec f : num -> num;
--- f 0 <= 1;
--- f 1 <= 1;
--- f(n+2) <= f n + f(n+1);
! Function f_print prepares a string with nth Fibonacci number for output
dec f_print : num -> list char;
--- f_print n <= concat [num2str n, "th Fibonacci number is ", num2str(f n)];
! Output
write [f_print 46];

ALGORITHM 2A-3: DATA STRUCTURE - SIMPLE LIST
uses lists;
! Infinite list f contains all Fibonacci numbers
! It uses ALGORITHM 2A-1: DATA STRUCTURE - INFINITE LIST
! This function was originally written by Ross Paterson, the author of Hope
dec f : list num;
--- f <= fs whererec fs == 1::1::map (+) (tail fs||fs);
! Function f_print prepares a string with nth Fibonacci number for output
dec f_print : num -> list char;
--- f_print n <= concat [num2str n, "th Fibonacci number is ", num2str(f@n)];
! Output
write [f_print 46];

ALGORITHM 2B: SIMPLE RECURSION
uses lists;
! Function f returns nth Fibonacci number
! It uses ALGORITHM 2B: SIMPLE RECURSION
dec f : num -> num;
--- f n <= l(1,1,n)
    whererec l == \(a,b,c) => if c<2 then a else l(a+b,a,c-1);
! Function f_print prepares a string with nth Fibonacci number for output
dec f_print : num -> list char;
--- f_print n <= concat [num2str n, "th Fibonacci number is ", num2str(f n)];
! Output
write [f_print 46];

ALGORITHM 2C: NON-RECURSIVE LOOP
Hope does not support non-recursive loops.
Rewriting this algorithm using recursion gives ALGORITHM 2B.

ALGORITHM 3A: MATRIX EQUATION
uses lists;
! Function mm multiplies two 2x2 matrices represented as 4-element lists
dec mm : list(num) # list(num) -> list(num);
--- mm([a11,a12,a21,a22],[b11,b12,b21,b22]) <=
  [a11*b11+a12*b21 ,a11*b12+a12*b22 ,a21*b11+a22*b21 ,a21*b12+a22*b22];
! Function mp raises a matrix into nth power
dec mp : list(num) # num -> list(num);
--- mp (A,n) <=
    if n>1 then 
        if (n mod 2)=1 then mm(mm(a,a),A) else mm(a,a)
    else A
    where a == mp(A, n div 2);
! Function f returns nth Fibonacci number
! It uses ALGORITHM 3A: MATRIX EQUATION
dec f : num -> num;
--- f n <= l@0 where l == mp([1,1,1,0],n);
! Function f_print prepares a string with nth Fibonacci number for output
dec f_print : num -> list char;
--- f_print n <= concat [num2str n, "th Fibonacci number is ", num2str(f n)];
! Output
write [f_print 46];

ALGORITHM 3B: FAST RECURSION
uses lists;
! Function f returns nth Fibonacci number
! It uses ALGORITHM 3B: FAST RECURSION
dec f : num -> num;
--- f n <= a where (a,b) == l(n)
    whererec l == \(n) =>
        if n<2 then (1,1) else
        if n=2 then (2,1) else
        if n mod 2 = 1 then
            (k1*(k1+k2)+k1*k2,k1*k1+k2*k2)
        else
            ((l1+l2)*(l1+l2)+l1*l1,(l1+l2)*l1+l1*l2)
        where (k1,k2) == l((n-1)/2)
        where (l1,l2) == l((n/2)-1);
! Function f_print prepares a string with nth Fibonacci number for output
dec f_print : num -> list char;
--- f_print n <= concat [num2str n, "th Fibonacci number is ", num2str(f n)];
! Output
write [f_print 46];

ALGORITHM 3C: BINET'S FORMULA
uses lists;
! Function f returns nth Fibonacci number
! It uses ALGORITHM 3C: BINET'S FORMULA
dec f : num -> num;
--- f n <= floor (pow(phi,(n+1))-pow((1-phi),(n+1)))/sqrt(5) 
           where phi == (1+sqrt(5))/2;
! Function f_print prepares a string with nth Fibonacci number for output
dec f_print : num -> list char;
--- f_print n <= concat [num2str n, "th Fibonacci number is ", num2str(f n)];
! Output
write [f_print 70];