From 90d61887627d84213e49554d050c6bc8e691715f Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Fri, 28 Oct 2011 11:24:17 +0200 Subject: [PATCH] hw1: done --- 1hw.fs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/1hw.fs b/1hw.fs index 1bbdc00..1ca6fae 100644 --- a/1hw.fs +++ b/1hw.fs @@ -134,6 +134,27 @@ : 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 ; -- 2.25.1