hyTags
HomeHome DocumentationDocumentation

Fibonacci Numbers

Overview

This example demonstrates recursive function definitions in hyTags. Enter a number and calculate its Fibonacci value.

The example shows how to define functions with parameters and return types, use variables, and handle form submissions.

<form>
  <input type="text" name="n" value="20">
  <button>Calculate</button>
</form>

<on submit>
  <form-get-value name="n">
  <number-from-string>
  <function-call name="fib"></function-call>
  <selection-scope next-sibling=".result">
    <number-to-string>
    <selection-set-text>
  </selection-scope>
</on>

<div class="result"></div>

<function name="fib" params="n: number" return="number">
  <number-is-equal other="0">
  <if-true>
    <function-return value="0">
  </if-true>
  <number-is-equal other="1">
  <if-true>
    <function-return value="1">
  </if-true>
  <var-set name="first">
    <number-subtract $self="n" value="1">
    <function-call name="fib"></function-call>
  </var-set>
  <var-set name="second">
    <number-subtract $self="n" value="2">
    <function-call name="fib"></function-call>
  </var-set>
  <number-add $self="first" $value="second">
</function>
👆 Try to change something! ⚠️ Preview only available on larger screens

Explanation

  • on submit: Attaches a submit event handler to the form. Runs when the form is submitted.
  • form-get-value: Gets the value of a form field by name. Pushes the value as a string onto the stack.
  • number-from-string: Converts the string on the stack to a number.
  • function-call: Calls a function by name. Arguments are taken from the stack based on the function's parameter types.
  • selection-scope: Temporarily changes the selection to execute commands on a different element (the result div).
  • number-to-string: Converts the number on the stack to a string.
  • selection-set-text: Sets the text content of the current selection to the string on the stack.
  • function: Defines a reusable function with typed parameters and return type. The function body contains the implementation.
  • var-set: Creates or updates a variable with the result of the code block inside.
  • function-return: Exits the current function and returns a value to the caller.
Test succeeded Test failed