74 lines
1.6 KiB
Text
74 lines
1.6 KiB
Text
; useful constants
|
|
make "false 1=0
|
|
make "true 1=1
|
|
make "lf char 10
|
|
make "sp char 32
|
|
|
|
; non-digits legal in expression
|
|
make "operators (lput sp [+ - * / \( \)])
|
|
|
|
; display help message
|
|
to show_help :digits
|
|
type lf
|
|
print sentence quoted [Using only these digits:] :digits
|
|
print sentence quoted [and these operators:] [* / + -]
|
|
print quoted [\(and parentheses as needed\),]
|
|
print quoted [enter an arithmetic expression
|
|
which evaluates to exactly 24.]
|
|
type lf
|
|
print quoted [Enter \"!\" to get fresh numbers.]
|
|
print quoted [Enter \"q\" to quit.]
|
|
type lf
|
|
end
|
|
|
|
make "digits []
|
|
make "done false
|
|
until [done] [
|
|
|
|
if empty? digits [
|
|
make "digits (map [(random 9) + 1] [1 2 3 4])
|
|
]
|
|
|
|
(type "Solution sp "for sp digits "? sp )
|
|
make "expression readrawline
|
|
|
|
ifelse [expression = "?] [
|
|
|
|
show_help digits
|
|
|
|
] [ifelse [expression = "q] [
|
|
|
|
print "Bye!
|
|
make "done true
|
|
|
|
] [ifelse [expression = "!] [
|
|
|
|
make "digits []
|
|
|
|
] [
|
|
make "exprchars ` expression
|
|
make "allowed (sentence digits operators)
|
|
|
|
ifelse (member? false (map [[c] member? c allowed] exprchars)) [
|
|
(print quoted [Illegal character in input.])
|
|
] [
|
|
catch "error [
|
|
make "syntax_error true
|
|
make "testval (run expression)
|
|
make "syntax_error false
|
|
]
|
|
ifelse syntax_error [
|
|
(print quoted [Invalid expression.])
|
|
] [
|
|
ifelse (testval = 24) [
|
|
print quoted [You win!]
|
|
make "done true
|
|
] [
|
|
(print (sentence
|
|
quoted [Incorrect \(got ] testval quoted [instead of 24\).]))
|
|
]
|
|
]
|
|
]
|
|
]]]
|
|
]
|
|
bye
|