compudanzas/src/forth.gmo
2021-07-15 21:14:02 -05:00

53 lines
1.2 KiB
Plaintext

# forth
stack-based and lightweight programming language.
it uses the {postfix} notation.
# reading
this book has a great pace and nice illustrations
=> https://www.forth.com/starting-forth starting forth book
# some words
## running words
these are some words for doing arithmetic with paces (min/km) and velocities (km/hr)
``` forth words
: ms>s swap 60 * + ; ( min segs -- segs )
: hms>s ms>s swap 3600 * + ; ( hr min segs -- segs )
: msd>s1k >r ms>s 1000 swap r> */ ; ( min segs metros -- segs1km )
: paso>vel ms>s s1k>vel ; ( min segs -- vel )
: s1k>vel 3600 swap / ; ( segs1km -- vel )
: vel>s1k 3600 swap / ; ( vel -- segs1km )
: .min 0 <# [CHAR] ' HOLD #S #> TYPE ; ( min -- )
: .seg 0 <# [CHAR] " HOLD # # #> TYPE ; ( segs -- )
: .ms 60 /mod .min .seg ; ( segs -- )
: .vel . ." km/hr " ;
: lista-velocidades cr 21 10 do i dup . vel>s1k .ms cr loop ;
```
for example, to convert a velocity to a pace, and print it:
``` input: 18, output: 3'20"
18 vel>s1k .ms
3'20" ok
```
to do the opposite operation:
``` input: 3 20, output: 18 km/hr
3 20 ms>s s1k>vel .vel
18 km/hr ok
```
to get the pace of a given segment, using minutes, seconds and distance in meters:
``` input: 1 03 300, output: 3'30"
1 03 300 msd>s1k .ms
3'30" ok
```