open System // Question 1. type Stack<'a> = | Elem of 'a * Stack<'a> | Empty member this.Push a = Elem (a, this) member this.Top = match this with | Elem (a, _) -> a | Empty -> failwith "Empty stack!" member this.Pop = match this with | Elem (_, rest) -> rest | Empty -> failwith "Empty stack!" override this.ToString () = match this with | Elem (e, rest) -> e.ToString() + "," + rest.ToString() | Empty -> "Empty" type Foo () = class end type Bar () = inherit Foo () let question1 () = printfn "Question 1" let a = (((Empty.Push "hello").Push "world").Push "it's fun").Pop printfn "a: %O" a assert (a.ToString () = "world,hello,Empty") let b = ((Empty.Push 1).Push 3) printfn "b.Top: %O" b.Top assert (b.Top = 3) let c : Stack = (Empty.Push <| Bar ()).Push <| Bar () c.Top :> Bar |> ignore c.Top :> Foo |> ignore let d : Stack<#Foo> = (Empty.Push <| Bar ()).Push <| Bar () d.Top :> Bar |> ignore // Question 4. let question4 () = let rec sieve sequence = seq { let h = Seq.head sequence yield h yield! sieve <| Seq.filter (fun m -> m % h <> 0) sequence } let primes = sieve <| (Seq.initInfinite id |> Seq.skip 2) printfn "first primes: %A" <| (Seq.take 100 primes |> Seq.toList) [] let main argv = question1 () question4 () 0