Skip to content

Instantly share code, notes, and snippets.

@tacryt-socryp
Last active May 16, 2018 22:42
Show Gist options
  • Save tacryt-socryp/c679c8d7d50b08a78036be9b821326da to your computer and use it in GitHub Desktop.
Save tacryt-socryp/c679c8d7d50b08a78036be9b821326da to your computer and use it in GitHub Desktop.
Addition written in Hoon
/+ mylib :: import
=, mylib :: alias to the subject (don't have to type mylib anymore)
|= [a=@ b=@]
^- @
(new-add a b)

Addition

Write a gate that takes two integers g and h and returns (g + h). You may not use add (or any variant thereof) from the Hoon stdlib, and you may not look at the code for it in hoon.hoon. You may use dec if you want, as long as you've already completed problem 1.

|%
++ new-add
|= a=[b=@ c=@]
^- @
%- inner-add :+(b.a c.a 0) :: call add function
++ inner-add
|= e=[a=@ b=[c=@ d=@]] :: sample (input mold). a.e = 1st operand, c.b.e = 2nd operand, d.b.e = result.
^- @ :: return atom
::~& e :: printf
?: (gte a.e 1) :: if a.e >= 1
%- inner-add :+((dec a.e) c.b.e .+(d.b.e)) :: yes, call inner-add with a tuple of (a.e - 1), c.b.e, and (d.b.e + 1)
?: (gte c.b.e 1) :: no, check if c.b.e >= 1
%- inner-add :+(a.e (dec c.b.e) .+(d.b.e)) :: yes, call inner-add with a tuple of a.e, (c.b.e - 1), and (d.b.e + 1)
d.b.e :: no, return result
--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment