Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,25 @@
|
|||
on run
|
||||
-- define the number to be guessed
|
||||
set numberToGuess to (random number from 1 to 10)
|
||||
-- prepare a variable to store the user's answer
|
||||
set guessedNumber to missing value
|
||||
-- start a loop (will be exited by using "exit repeat" after a correct guess)
|
||||
repeat
|
||||
try
|
||||
-- ask the user for his/her guess
|
||||
set usersChoice to (text returned of (display dialog "Guess the number between 1 and 10 inclusive" default answer "" buttons {"Check"} default button "Check"))
|
||||
-- try to convert the given answer to an integer
|
||||
set guessedNumber to usersChoice as integer
|
||||
on error
|
||||
-- something gone wrong, overwrite user's answer with a non-matching value
|
||||
set guessedNumber to missing value
|
||||
end try
|
||||
-- decide if the user's answer was the right one
|
||||
if guessedNumber is equal to numberToGuess then
|
||||
-- the user guessed the correct number and gets informed
|
||||
display dialog "Well guessed! The number was " & numberToGuess buttons {"OK"} default button "OK"
|
||||
-- exit the loop (quits this application)
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
end run
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
-- GUESS THE NUMBER ----------------------------------------------------------
|
||||
|
||||
on run
|
||||
-- isMatch :: Int -> Bool
|
||||
script isMatch
|
||||
on |λ|(x)
|
||||
tell x to its guess = its secret
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- challenge :: () -> {secret: Int, guess: Int}
|
||||
script challenge
|
||||
on response()
|
||||
set v to (text returned of (display dialog ¬
|
||||
"Guess the number in range 1-10" default answer ¬
|
||||
"" buttons {"Esc", "Check"} default button ¬
|
||||
"Check" cancel button "Esc"))
|
||||
|
||||
if isInteger(v) then
|
||||
v as integer
|
||||
else
|
||||
-1
|
||||
end if
|
||||
end response
|
||||
|
||||
on |λ|(rec)
|
||||
{secret:(random number from 1 to 10), guess:response() ¬
|
||||
of challenge, attempts:(attempts of rec) + 1}
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
|
||||
-- MAIN LOOP -------------------------------------------------------------
|
||||
set rec to |until|(isMatch, challenge, {secret:-1, guess:0, attempts:0})
|
||||
|
||||
display dialog (((guess of rec) as string) & ": Well guessed ! " & ¬
|
||||
linefeed & linefeed & "Attempts: " & (attempts of rec))
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ---------------------------------------------------------
|
||||
|
||||
-- isInteger :: a -> Bool
|
||||
on isInteger(e)
|
||||
try
|
||||
set n to e as integer
|
||||
on error
|
||||
return false
|
||||
end try
|
||||
true
|
||||
end isInteger
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
on |until|(p, f, x)
|
||||
set mp to mReturn(p)
|
||||
set v to x
|
||||
|
||||
tell mReturn(f)
|
||||
repeat until mp's |λ|(v)
|
||||
set v to |λ|(v)
|
||||
end repeat
|
||||
end tell
|
||||
return v
|
||||
end |until|
|
||||
Loading…
Add table
Add a link
Reference in a new issue