hw1: done
[sbs.git] / 1hw.fs
diff --git a/1hw.fs b/1hw.fs
index 1bbdc00850b2ca1221af777678f522f40f2b29d1..1ca6fae71f56562dd7e88579c5dfe205736f2dbe 100644 (file)
--- a/1hw.fs
+++ b/1hw.fs
 
 : show20fib cr 20 0 u+do i dup dup . ." : " fibit . ." , " fibrec . cr loop ;
 
-\ TODO
 \ ============================== 3.22 ========================================
+\ Assignment: Can you rewrite any of the definitions you wrote until now in a
+\ better way using the return stack?
+: min ( n1 n2 -- n)
+       2dup < >r r@ ( n1 n2 v )
+       invert and swap r> and or ;
+
 \ ============================== 3.23 ========================================
+\ Assignment: Write a definition sum ( addr u -- n ) that computes the sum of u
+\ cells, with the first of these calls at addr, the next one at addr cell+ etc.
+: sum ( addr u -- n )
+       >r
+       1 cells - 0 ( addr-4 0)
+       r> 0 ( addr-4 0 u 0 )
+       u+do
+               swap cell+ dup ( 0 addr addr )
+               @ ( 0 addr val )
+               rot ( addr val 0 )
+               + ( addr sum )
+       loop swap drop ;
+
+create v3
+ 5 , 4 , 3 , 2 , 1 ,
+: sumexample v3 5 sum ;