2 people like it.
Like the snippet!
Magic 8 Ball
Magic 8 Ball in your console/terminal
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
|
open System
type Rnd = { rnd : Random }
let createRndWithSeed (seed) = { rnd = new Random(seed) }
let inline precondition cond dueTo =
if (not cond) then
failwith ("precondition violation: " + dueTo)
//[0, 1)
let uniform (r : Rnd) = r.rnd.NextDouble()
//[0, n)
let uniformInt (r : Rnd) n =
precondition (n > 0) "less or equal than 0"
r.rnd.Next(n)
//[n, m)
let uniformRange (r : Rnd) n m =
precondition (m > n) "invalid range"
n + uniform r * (m - n)
//[n, m)
let uniformIntRange (r : Rnd) n m =
precondition (m > n) "invalid range"
precondition (int64 (m - n) < int64 Int32.MaxValue) "invalid range"
n + uniformInt r (m - n)
let shuffle (r : Rnd) (xs : 'a array) =
precondition (xs <> null) "array is null"
let n = xs |> Array.length
for i = 0 to n - 1 do
let j = i + uniformInt r (n - i)
let temp = xs.[i]
xs.[i] <- xs.[j]
xs.[j] <- temp
//Magic 8 Ball (https://en.wikipedia.org/wiki/Magic_8-Ball)
let magic8Ball () =
let magic8BallAnswers =
[|
//positive
"It is certain";
"It is decidedly so";
"Without a doubt";
"Yes, definitely";
"You may rely on it";
"As I see it, yes";
"Most likely";
"Outlook good";
"Yes";
"Signs point to yes";
//neutral
"Reply hazy try again";
"Ask again later";
"Better not tell you now";
"Cannot predict now";
"Concentrate and ask again";
//negative
"Don't count on it";
"My reply is no";
"My sources say no";
"Outlook not so good";
"Very doubtful"
|]
let rec loop (f : unit -> int) =
Console.Title <- "The wonderful Magic 8 Ball. Ask me something..."
printfn "Magic ball: What's your question?"
printf "You: "
let ask = Console.ReadLine()
if String.IsNullOrEmpty(ask) then ()
else
printfn "Magic ball: %s" magic8BallAnswers.[f ()]
loop f
let n = magic8BallAnswers |> Array.length
let now = DateTime.Now
let r = createRndWithSeed (now.Millisecond + now.Second * 1000)
magic8BallAnswers |> shuffle r
loop (fun () -> uniformIntRange r 0 n)
magic8Ball ()
(*
Magic ball: What's your question?
You: Will I be rich?
Magic ball: My sources say no
Magic ball: What's your question?
You: Will I be famous?
Magic ball: Concentrate and ask again
Magic ball: What's your question?
You: Will I be famous?
Magic ball: Cannot predict now
Magic ball: What's your question?
You: Will my team win the championship?
Magic ball: Very doubtful
Magic ball: What's your question?
You: Will I win the first prize?
Magic ball: Signs point to yes
Magic ball: What's your question?
You:
*)
|
namespace System
type Rnd =
{rnd: Random;}
Full name: Script.Rnd
Rnd.rnd: Random
Multiple items
type Random =
new : unit -> Random + 1 overload
member Next : unit -> int + 2 overloads
member NextBytes : buffer:byte[] -> unit
member NextDouble : unit -> float
Full name: System.Random
--------------------
Random() : unit
Random(Seed: int) : unit
val createRndWithSeed : seed:int -> Rnd
Full name: Script.createRndWithSeed
val seed : int
val precondition : cond:bool -> dueTo:string -> unit
Full name: Script.precondition
val cond : bool
val dueTo : string
val not : value:bool -> bool
Full name: Microsoft.FSharp.Core.Operators.not
val failwith : message:string -> 'T
Full name: Microsoft.FSharp.Core.Operators.failwith
val uniform : r:Rnd -> float
Full name: Script.uniform
val r : Rnd
Random.NextDouble() : float
val uniformInt : r:Rnd -> n:int -> int
Full name: Script.uniformInt
val n : int
Random.Next() : int
Random.Next(maxValue: int) : int
Random.Next(minValue: int, maxValue: int) : int
val uniformRange : r:Rnd -> n:float -> m:float -> float
Full name: Script.uniformRange
val n : float
val m : float
val uniformIntRange : r:Rnd -> n:int -> m:int -> int
Full name: Script.uniformIntRange
val m : int
Multiple items
val int64 : value:'T -> int64 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int64
--------------------
type int64 = Int64
Full name: Microsoft.FSharp.Core.int64
--------------------
type int64<'Measure> = int64
Full name: Microsoft.FSharp.Core.int64<_>
type Int32 =
struct
member CompareTo : value:obj -> int + 1 overload
member Equals : obj:obj -> bool + 1 overload
member GetHashCode : unit -> int
member GetTypeCode : unit -> TypeCode
member ToString : unit -> string + 3 overloads
static val MaxValue : int
static val MinValue : int
static member Parse : s:string -> int + 3 overloads
static member TryParse : s:string * result:int -> bool + 1 overload
end
Full name: System.Int32
field int.MaxValue = 2147483647
val shuffle : r:Rnd -> xs:'a array -> unit (requires equality)
Full name: Script.shuffle
val xs : 'a array (requires equality)
type 'T array = 'T []
Full name: Microsoft.FSharp.Core.array<_>
type Array =
member Clone : unit -> obj
member CopyTo : array:Array * index:int -> unit + 1 overload
member GetEnumerator : unit -> IEnumerator
member GetLength : dimension:int -> int
member GetLongLength : dimension:int -> int64
member GetLowerBound : dimension:int -> int
member GetUpperBound : dimension:int -> int
member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
member Initialize : unit -> unit
member IsFixedSize : bool
...
Full name: System.Array
val length : array:'T [] -> int
Full name: Microsoft.FSharp.Collections.Array.length
val i : int
val j : int
val temp : 'a (requires equality)
val magic8Ball : unit -> unit
Full name: Script.magic8Ball
val magic8BallAnswers : string []
val loop : ((unit -> int) -> unit)
val f : (unit -> int)
type unit = Unit
Full name: Microsoft.FSharp.Core.unit
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
type Console =
static member BackgroundColor : ConsoleColor with get, set
static member Beep : unit -> unit + 1 overload
static member BufferHeight : int with get, set
static member BufferWidth : int with get, set
static member CapsLock : bool
static member Clear : unit -> unit
static member CursorLeft : int with get, set
static member CursorSize : int with get, set
static member CursorTop : int with get, set
static member CursorVisible : bool with get, set
...
Full name: System.Console
property Console.Title: string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val printf : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
val ask : string
Console.ReadLine() : string
Multiple items
type String =
new : value:char -> string + 7 overloads
member Chars : int -> char
member Clone : unit -> obj
member CompareTo : value:obj -> int + 1 overload
member Contains : value:string -> bool
member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
member EndsWith : value:string -> bool + 2 overloads
member Equals : obj:obj -> bool + 2 overloads
member GetEnumerator : unit -> CharEnumerator
member GetHashCode : unit -> int
...
Full name: System.String
--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
String.IsNullOrEmpty(value: string) : bool
val now : DateTime
Multiple items
type DateTime =
struct
new : ticks:int64 -> DateTime + 10 overloads
member Add : value:TimeSpan -> DateTime
member AddDays : value:float -> DateTime
member AddHours : value:float -> DateTime
member AddMilliseconds : value:float -> DateTime
member AddMinutes : value:float -> DateTime
member AddMonths : months:int -> DateTime
member AddSeconds : value:float -> DateTime
member AddTicks : value:int64 -> DateTime
member AddYears : value:int -> DateTime
...
end
Full name: System.DateTime
--------------------
DateTime()
(+0 other overloads)
DateTime(ticks: int64) : unit
(+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
(+0 other overloads)
property DateTime.Now: DateTime
property DateTime.Millisecond: int
property DateTime.Second: int
More information