All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sampleSpecs.Fibonacci.coreasm Maven / Gradle / Ivy

/*
 * Recursion in CoreASM
 */ 

CoreASM Fibonacci

use Standard

init InitRule

rule InitRule = 
	let n = 10 in 
		if steps = undef then {
			print "Fibonacci(" + n + ") using dynamic programming: " + fibo_d(n) + "\nNow computing it with recursion..."
			steps := 1
		} else {
			print "Fibonacci(" + n + ") using pure recursion: " + fibo_r(n) 
			program(self) := undef
		}


derived fibo_r(x) = 
	local r in return r in 
		if x < 0 then r := 0
		else if x < 2 then r := x
		else r := fibo_r(x-2) + fibo_r(x-1) 
		
derived fibo_d(x) = 
	return fibo(x) in 
		if fibo(x) = undef then { 
			if x < 0 then fibo(x) := 0
			else if x < 2 then fibo(x) := x
			else fibo(x) := fibo_d(x-2) + fibo_d(x-1)
		}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy