#lang planet wrturtle/pyret/bsl # examples/fun.rkt # Shows the usage of fun # `fun' in one-line fun det_one_line(a,b,c): b ** 2 - 4 * a * c # Should be 16 test: det_one_line(1,6,5) is: 16 # `fun' with local definitions needs more # than one line fun det(a,b,c): def b2: b * b def ac4: 4 * a * c b2 - ac4 # Should also be 16 test: det(1,6,5) is: 16 fun det_with_helpers(a,b,c): fun b2(b): b * b fun ac4(a,c): a * c * 4 b2(b) - ac4(a,c) # You know by now... test: det_with_helpers(1,6,5) is: 16 # Ye olde factorial function fun fac(n): if is_zero(n): 1 else: n * fac(sub1(n)) test: fac(0) is: 1 test: fac(1) is: 1 test: fac(4) is: 24 test: fac(5) is: 120