Классы типов Haskell и классы шаблонов C ++

Можно ли имитировать функциональность классов типов в Haskell с помощью шаблонов C ++ (или C #)?

Есть ли в этом смысл или есть ли какая-то выгода от этого? что?

Я пытался создать класс Functor на C ++, но не смог. Я пробовал что-то вроде этого:

#include <iostream>
using namespace std;

//A function class to make types more readable
template <class input, class output> class Function {
private:
  output (*ptrfunc )(input);
public:
  Function(output (* ptr)(input)) {
    ptrfunc = ptr;
  }
  output call(input x) {return  (*ptrfunc)(x);}
  output operator() (input x) { return call(x);}
};


//the functor "typeclass"
template <class a> class Functor{
public:
  template <class b> Functor<b> fmap(Function<a,b> func);
};

// an container type to be declared "instance" of functor:
template <class a> class List : public Functor<a> { 
private:
  a * ptrList;
  int size;
public:
  List(int n) {  //constructor;
    ptrList = new a[n]; 
    size = n;
  }
  List(List<a> const& other) { //copy constructor
    size = other.size;
    ptrList = new a[size];
    for(int i = 0; i<size; i++)
      (*this)[i] = other[i];
  }
  ~List() { delete ptrList;} //destructor
  a& operator[](int i) { return ptrList[i];} // subscript operator just for easy notation
  const a& operator[](int i) const { return ptrList[i];}// subscript operator just for easy notation

  template <class b> List<b> fmap(Function<a,b> func) { //"instance" version of fmap
    List<b> temp(size);
    for(int i = 0; i < size; i++)
      temp[i] = func((*this)[i]);
    return temp;
  }
};


int test(int k) { return 2 * k;}

int main(void) {
  Function<int, int> func(&test);
  List<int> lista(10);
  for(int i = 0; i < 10; i++)
    lista[i] = i;
  List<int> lista2(lista.fmap(func));
  for(int i = 0; i < 10; i++)
    cout << lista2[i] << " ";
  cout << endl;
  return 0;
}

Он делает то, что должен делать, но имеет ли смысл использовать этот шаблон в C ++? Действительно ли это тот же шаблон, что и в haskell:

data List a = -- some stuff 

class  Functor f  where
fmap :: (a -> b) -> f a -> f b

instance (Functor List) where
-- some stuff    

Мне это не кажется таким же, потому что в Functor f , f является разновидностью * -> * конструктор типа, и в моем определении выше в Functor , a не является шаблоном a , но сам "содержащийся" тип данных.

Есть ли выход из этого? И что еще важнее: есть ли смысл пытаться скопировать такие шаблоны на C ++? Мне кажется, что C # больше похож на стиль функционального программирования, чем C ++. Есть ли способ сделать это в C #?

21
задан Rafael S. Calsaverini 10 December 2010 в 16:53
поделиться