fork download
  1. using System;
  2. interface ScalarProduct<A> {
  3. int scalarProduct(A second);
  4. }
  5. class Nil : ScalarProduct<Nil> {
  6. public Nil(){}
  7. public int scalarProduct(Nil second) {
  8. return 0;
  9. }
  10. }
  11. class Cons<A> : ScalarProduct<Cons<A>> where A : ScalarProduct<A> {
  12. public int value;
  13. public A tail;
  14. public Cons(int _value, A _tail) {
  15. value = _value;
  16. tail = _tail;
  17. }
  18. public int scalarProduct(Cons<A> second){
  19. return value * second.value + tail.scalarProduct(second.tail);
  20. }
  21. }
  22. class _Test{
  23. public static int main(int n){
  24. return _main(n, 0, new Nil(), new Nil());
  25. }
  26. public static int _main<A>(int n, int i, A first, A second) where A : ScalarProduct<A> {
  27. if (n == 0) {
  28. return first.scalarProduct(second);
  29. } else {
  30. return _main(n-1, i+1, new Cons<Cons<A>>(i, new Cons<A>(2*i+1, new Nil())), new Cons<Cons<A>>(i*i, new Cons<A>(i, second))); // Works
  31. //return _main(n-1, i+1, new Cons<A>(2*i+1,first), new Cons<A>(i*i, second)); // Works
  32. //return _main(n-1, i+1, first, new Cons<A>(i*i, second)); // Doesn't work
  33. }
  34. }
  35. }
  36. public class Test{
  37. public static void Main(){
  38. Console.Write("Enter a number: ");
  39. int val = Convert.ToInt32(Console.ReadLine());
  40. Console.WriteLine(_Test.main(val));
  41. }
  42. }
Compilation error #stdin compilation error #stdout 0.04s 24232KB
stdin
3
compilation info
prog.cs(30,51): error CS1502: The best overloaded method match for `Cons<A>.Cons(int, A)' has some invalid arguments
prog.cs(14,10): (Location of the symbol related to previous error)
prog.cs(30,70): error CS1503: Argument `#2' cannot convert `Nil' expression to type `A'
Compilation failed: 2 error(s), 0 warnings
stdout
Standard output is empty