tag:blogger.com,1999:blog-4757118446768919900.post4694062841806937818..comments2021-08-04T12:25:03.832-07:00Comments on Retro Programming: Implement MIN in Forth without Conditional CodeJohn Metcalfhttp://www.blogger.com/profile/09108374348083307900noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-4757118446768919900.post-73054922623311993932013-10-02T16:44:09.642-07:002013-10-02T16:44:09.642-07:00One word shorter than my previous anonymous answer...One word shorter than my previous anonymous answers:<br />: min 2dup > and tuck 0= and or ;<br /><br />With commented stack as we go, where x/y means x if > was true, y if not:<br /><br />: min ( a b -- x )<br /> 2dup > ( a b t/f )<br /> and ( a b/0 )<br /> tuck 0= ( b/0 a f/t )<br /> and ( b/0 0/a )<br /> or ; ( b/a )Maxhttps://www.blogger.com/profile/05167248447070038908noreply@blogger.comtag:blogger.com,1999:blog-4757118446768919900.post-7190674289378384932013-10-02T15:31:12.241-07:002013-10-02T15:31:12.241-07:00As an explanation: ands true with the smaller one,...As an explanation: ands true with the smaller one, ands false with the larger one (making it 0), then ors those two together, to get the smaller one.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4757118446768919900.post-20387971735349951722013-10-02T15:28:44.544-07:002013-10-02T15:28:44.544-07:00With bitwise logic only:
: min ( a b - x ) 2dup &l...With bitwise logic only:<br />: min ( a b - x ) 2dup < tuck 0= and -rot and or ;Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4757118446768919900.post-33426725248471496462013-05-26T04:17:01.243-07:002013-05-26T04:17:01.243-07:00Thanks! I was trying to do it without pick - just ...Thanks! I was trying to do it without pick - just with comparisons and bitwise logic, which I don't think is possible.<br /><br />Impressive solutions!Kernel Blinghttps://www.blogger.com/profile/15474302847598356519noreply@blogger.comtag:blogger.com,1999:blog-4757118446768919900.post-46907720885709981872011-06-16T04:50:46.299-07:002011-06-16T04:50:46.299-07:00This discussion was a great help working through t...This discussion was a great help working through the tutorial in the Gforth manual.<br /><br />However, I think there is an error in John's solution: the conditional is in the wrong direction.<br /><br />John wrote:<br /><br /> : min 2dup < 1+ roll nip ; <br /><br />... but I believe the correct solution is:<br /><br /> : min 2dup > 1+ roll nip ;papahttp://papa.motd.org/cgi-bin/blosxom.cginoreply@blogger.comtag:blogger.com,1999:blog-4757118446768919900.post-27082142930439635542009-06-08T00:23:00.095-07:002009-06-08T00:23:00.095-07:00Anonymous: You're right. Thank you for finding...Anonymous: You're right. Thank you for finding my error.llogiqnoreply@blogger.comtag:blogger.com,1999:blog-4757118446768919900.post-41828660547346285322009-06-03T18:00:16.694-07:002009-06-03T18:00:16.694-07:00Er...what Forth system are you using? Admittedly ...Er...what Forth system are you using? Admittedly 2nip is not a standard word, but usually it is a double cell version of nip, not "nip nip", so 2dup > 1+ pick 2nip would not be a solution...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4757118446768919900.post-29139545035006509252009-06-03T16:25:49.119-07:002009-06-03T16:25:49.119-07:00Great - in fact I like your solution more than my ...Great - in fact I like your solution more than my own. Btw. I did not "get" forth, until researching it a little bit for solving this small puzzle and finding Rich Jones' impressively documented implementation. Thank you.llogiqnoreply@blogger.comtag:blogger.com,1999:blog-4757118446768919900.post-82543140083573898482009-06-03T08:31:01.001-07:002009-06-03T08:31:01.001-07:00llogiq: great solution, mine is similar:
: min 2d...llogiq: great solution, mine is similar:<br /><br />: min 2dup < 1+ roll nip ;Johnhttp://retrocode.blogspot.comnoreply@blogger.comtag:blogger.com,1999:blog-4757118446768919900.post-30235832512113474332009-06-03T01:42:03.373-07:002009-06-03T01:42:03.373-07:00: min 2dup > 1+ pick 2nip ;: min 2dup > 1+ pick 2nip ;llogiqnoreply@blogger.comtag:blogger.com,1999:blog-4757118446768919900.post-3736251914417315832009-06-01T22:11:56.087-07:002009-06-01T22:11:56.087-07:00tardieu: that's a neat solution. My technique look...tardieu: that's a neat solution. My technique looks ugly by comparison.<br /><br />However, it's possible to implement MIN in less words :-)Johnhttp://retrocode.blogspot.comnoreply@blogger.comtag:blogger.com,1999:blog-4757118446768919900.post-71870103387517324242009-06-01T14:55:18.273-07:002009-06-01T14:55:18.273-07:00: min 2dup - abs - + 2 / ;
If a < b, |a-b| = b...: min 2dup - abs - + 2 / ;<br /><br />If a < b, |a-b| = b-a, so min = a = ((a+b)-(b-a))/2 = (a+b)-|a-b|)/2.<br /><br />If b < a, |a-b| = a-b, so min = b = ((a+b)-(a-b))/2 = (a+b)-|a-b|)/2.<br /><br />So ((a+b)-|a-b|)/2 is always the right answer. 7 words, 6 if you have a builtin 2/.Anonymousnoreply@blogger.com