Gepostet: |
Haskell problems | ||||||||||
Hi I have three problems about haskell, 1.Compare the polymorphic, higher-order and laziness features of Haskell with Java or C/C++ , which is the better? Are there some good examples. 2. On the features of Type class of Haskell and its application (Please give me some examples) 3. which kind of the examples can reflect the features of Haskell. thanks |
|||||||||||
Zum Seitenanfang | |||||||||||
Gepostet: |
|||||||||||
1) example in C++ template<class T> void swap(T& x, T& y) { T temp = x; x = y; y = temp; } appliance: int i,j; swap<int> (i,j); //a swap for int char i,j; swap<char>(i,j); //a swap for char without explicit datatype java example: java.util.Stack class class Stack<E> extends Vector<E> { public E push(E item) {..} public synchronized E pop() {..} public synchronized E peek() {..} public boolean empty() {..} public synchronized int search(Object o) {..} } appliance: Stack<String> foo = new Stack<String>(); method genericity: static <Elem> void swap(Elem[] array, int x, int y) { Elem temp = array[x]; array[x] = array[y]; array[y] = temp; } haskell swap (x,y) =(y,x) shorter ... 2) in haskell you can create own types example: type Person=String type Book= String type Database=[(Person, Book)] --list of tupels from persons and Books makeloan::Database->Person->Book->Database makeloan examplebase (aperson,abook)=examplebase++[(aperson,abook)] ------------------------------------------------------------------------- 3) also polymorphism fst (x,y)=x snd(x,y)=y |
|||||||||||
Zum Seitenanfang | |||||||||||