6.1.1880 MIN CORE

( n1 n2 -- n3 )

n3 is the lesser of n1 and n2.

Testing:

T{       0       1 MIN ->       0 }T
T{       1       2 MIN ->       1 }T
T{      -1       0 MIN ->      -1 }T
T{      -1       1 MIN ->      -1 }T
T{ MIN-INT       0 MIN -> MIN-INT }T
T{ MIN-INT MAX-INT MIN -> MIN-INT }T
T{       0 MAX-INT MIN ->       0 }T
T{       0       0 MIN ->       0 }T
T{       1       1 MIN ->       1 }T
T{       1       0 MIN ->       0 }T
T{       2       1 MIN ->       1 }T
T{       0      -1 MIN ->      -1 }T
T{       1      -1 MIN ->      -1 }T
T{       0 MIN-INT MIN -> MIN-INT }T
T{ MAX-INT MIN-INT MIN -> MIN-INT }T
T{ MAX-INT       0 MIN ->       0 }T

ContributeContributions

JimPetersonavatar of JimPeterson [233] Possible Reference ImplementationSuggested reference implementation2022-04-05 14:43:43

I'm not sure of the value of reference implementations for such simple words, but here's one:

: MIN 2DUP < IF DROP EXIT THEN NIP ;

... there's also an equivalent for MAX:

: MAX 2DUP < IF NIP EXIT THEN DROP ;

But hen, NIP is in CORE-EXT, and providing a reference for a CORE word that uses words from CORE-EXT may seem invalid, so would these be more preferred?:

: MIN 2DUP > IF SWAP THEN DROP ;
: MAX 2DUP < IF SWAP THEN DROP ;

ruvavatar of ruv

providing a reference for a CORE word that uses words from CORE-EXT may seem invalid

It's not invalid for a reference implementation.

A reference implementation for a word is just an illustration how this word can be implemented, or what algorithm can be used for that.

See also a similar question (and my answer there).

Reply New Version