projects
/
sbs.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
dc5cdbb
)
hw1: done
author
Bernhard Urban
<lewurm@gmail.com>
Fri, 28 Oct 2011 09:24:17 +0000
(11:24 +0200)
committer
Bernhard Urban
<lewurm@gmail.com>
Fri, 28 Oct 2011 09:24:17 +0000
(11:24 +0200)
1hw.fs
patch
|
blob
|
history
diff --git
a/1hw.fs
b/1hw.fs
index 1bbdc00850b2ca1221af777678f522f40f2b29d1..1ca6fae71f56562dd7e88579c5dfe205736f2dbe 100644
(file)
--- a/
1hw.fs
+++ b/
1hw.fs
@@
-134,6
+134,27
@@
: show20fib cr 20 0 u+do i dup dup . ." : " fibit . ." , " fibrec . cr loop ;
: show20fib cr 20 0 u+do i dup dup . ." : " fibit . ." , " fibrec . cr loop ;
-\ TODO
\ ============================== 3.22 ========================================
\ ============================== 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 ========================================
\ ============================== 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 ;