Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Last active December 25, 2015 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akimboyko/7010571 to your computer and use it in GitHub Desktop.
Save akimboyko/7010571 to your computer and use it in GitHub Desktop.
Quiz from Functional Programming Principles in Scala by Martin Odersky, lecture 4.2 rewritten on C#/F#

Provide an implementation of the abstract class Nat that represents non-negative integers

Do not use standard numerical classes in this implementation. Rather, implement a sub-object and sub-class:

class Zero : Nat
class Succ(n: Nat) : Nat

One of the number zero, then other for strictly positive numbers.

//Provide an implementation of the abstract class Nat that represents non-negative integers
//
//Do not use standard numerical classes in this implementation.
//Rather, implement two sub-classes:
//
//class Zero : Nat
//class Succ(n: Nat) : Nat
//
//One of the number zero, then other for strictly positive numbers.
interface Nat {
bool IsZero();
Nat Predecessor();
Nat Successor();
Nat Plus(Nat that);
Nat Minus(Nat that);
}
class Zero : Nat
{
public bool IsZero() {
throw new NotImplementedException();
}
public Nat Predecessor() {
throw new NotImplementedException();
}
public Nat Successor() {
throw new NotImplementedException();
}
public Nat Plus(Nat that) {
throw new NotImplementedException();
}
public Nat Minus(Nat that) {
throw new NotImplementedException();
}
}
class Succ : Nat
{
public bool IsZero() {
throw new NotImplementedException();
}
public Nat Predecessor() {
throw new NotImplementedException();
}
public Nat Successor() {
throw new NotImplementedException();
}
public Nat Plus(Nat that) {
throw new NotImplementedException();
}
public Nat Minus(Nat that) {
throw new NotImplementedException();
}
}
//Provide an implementation of the abstract class Nat that represents non-negative integers
//
//Do not use standard numerical classes in this implementation.
//Rather, implement a sub-object and sub-class:
//
//class Zero : Nat
//class Succ(n: Nat) : Nat
//
//One of the number zero, then other for strictly positive numbers.
namespace Nat
module Nat =
open System
type Nat =
abstract member IsZero : unit -> bool
abstract member Predecessor : unit -> Nat
abstract member Successor : unit -> Nat
abstract member Plus : Nat -> Nat
abstract member Minus : Nat -> Nat
type Succ(n: Nat) =
interface Nat with
member this.IsZero() = raise(NotImplementedException())
member this.Predecessor() = raise(NotImplementedException())
member this.Successor() = raise(NotImplementedException())
member this.Plus(that) = raise(NotImplementedException())
member this.Minus(that) = raise(NotImplementedException())
let Zero = {
new Nat with
member this.IsZero() = raise(NotImplementedException())
member this.Predecessor() = raise(NotImplementedException())
member this.Successor() = raise(NotImplementedException())
member this.Plus(that) = raise(NotImplementedException())
member this.Minus(that) = raise(NotImplementedException())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment