# step 1 sub binary { my ($n) = @_; return $n if $n == 0 || $n == 1; #step 2 my $k = int($n/2); my $b = $n % 2; #step 3, recursion my $E = binary($k); #step 4 return $E . $b; } #### # step 1 sub binary ($n) { return $n if $n == 0|1; #step 2 my $k = int($n/2); my $b = $n % 2; #step 3, recursion my $E = binary($k); #step 4 return $E ~ $b; } #### sub binary (0|1 $n) { $n } sub binary ($n) { my $k = int($n/2); my $b = $n % 2; my $E = binary($k); return $E ~ $b; } #### sub binary (Int where { $_ < 0 } $n) { my $E= binary(-$n); return "-" ~ $E; } #### subset NegativeInt of Int where { $_ < 0 } # I decided to declare it after all subset PositiveInt of Int where { $_ > 0 } sub binary (NegativeInt $n, :$wordsize) { if defined $wordsize { # do 2's complement mode PositiveInt $m= 2 ** $wordsize - $n; return binary($m); } else { return "-" ~ binary(-$n); } } #### PRE { !defined($wordsize) || frac($wordsize)==0 }