Skip to main content
grammar error
Source Link
Manoj R
  • 4.1k
  • 25
  • 30

In Haskell, the closest you have is "class". This class though not as same as the class in Java and C++ , will work for what you want in this case.

In your case this is how your code will look.

class Animal a where 
say :: String -> sound 

Then you can have individual data types adapting these methods.

instance Animal Dog where
say s = "bark " ++ s 

EDIT :- Before you can specialize say for Dog you need to tell the system that Dog is animal.

data Dog =  \--something here --\ (deriving animalAnimal)

EDIT :- For Wilq.
Now if you want to use say in a function say foo, you will have to tell haskell that foo can only work with Animal.

foo :: (Animal a) => a -> String -> String
foo a str = say a str 

now if you call foo with dog it will bark, if you call with cat it will meow.

main = do 
let d = dog (\--cstr parameters--\)
    c = cat  
in show $ foo d "Hello World"

You now can not have any other definition of function say. If say is called with anything which is not animal, it will cause compile error.

In Haskell, the closest you have is "class". This class though not as same as the class in Java and C++ , will work for what you want in this case.

In your case this is how your code will look.

class Animal a where 
say :: String -> sound 

Then you can have individual data types adapting these methods.

instance Animal Dog where
say s = "bark " ++ s 

EDIT :- Before you can specialize say for Dog you need to tell the system that Dog is animal.

data Dog =  \--something here --\ (deriving animal)

EDIT :- For Wilq.
Now if you want to use say in a function say foo, you will have to tell haskell that foo can only work with Animal.

foo :: (Animal a) => a -> String -> String
foo a str = say a str 

now if you call foo with dog it will bark, if you call with cat it will meow.

main = do 
let d = dog (\--cstr parameters--\)
    c = cat  
in show $ foo d "Hello World"

You now can not have any other definition of function say. If say is called with anything not animal will cause compile error.

In Haskell, the closest you have is "class". This class though not as same as the class in Java and C++ , will work for what you want in this case.

In your case this is how your code will look.

class Animal a where 
say :: String -> sound 

Then you can have individual data types adapting these methods.

instance Animal Dog where
say s = "bark " ++ s 

EDIT :- Before you can specialize say for Dog you need to tell the system that Dog is animal.

data Dog =  \--something here --\ (deriving Animal)

EDIT :- For Wilq.
Now if you want to use say in a function say foo, you will have to tell haskell that foo can only work with Animal.

foo :: (Animal a) => a -> String -> String
foo a str = say a str 

now if you call foo with dog it will bark, if you call with cat it will meow.

main = do 
let d = dog (\--cstr parameters--\)
    c = cat  
in show $ foo d "Hello World"

You now can not have any other definition of function say. If say is called with anything which is not animal, it will cause compile error.

added 555 characters in body
Source Link
Manoj R
  • 4.1k
  • 25
  • 30

In Haskell, the closest you have is "class". This class though not as same as the class in Java and C++ , will work for what you want in this case.

In your case this is how your code will look.

class Animal a where 
say :: String -> sound 

Then you can have individual data types adapting these methods.

instance Animal Dog where
say s = "bark " ++ s 

EDIT :- Before you can specialize say for Dog you need to tell the system that Dog is animal.

data Dog =  \--something here --\ (deriving animal)

EDIT :- For Wilq.
Now if you want to use say in a function say foo, you will have to tell haskell that foo can only work with Animal.


foo :: (Animal a) => a -> String -> String
foo a str = say a str 

now if you call foo with dog it will bark, if you call with cat it will meow.


main = do 
let d = dog (\--cstr parameters--\)
    c = cat  
in show $ foo d "Hello World"

You now can not have any other definition of function say. If say is called with anything not animal will cause compile error.

In Haskell, the closest you have is "class". This class though not as same as the class in Java and C++ , will work for what you want in this case.

In your case this is how your code will look.

class Animal a where 
say :: String -> sound 

Then you can have individual data types adapting these methods.

instance Animal Dog where
say s = "bark " ++ s 

EDIT :- Before you can specialize say for Dog you need to tell the system that Dog is animal.

data Dog =  \--something here --\ (deriving animal)

In Haskell, the closest you have is "class". This class though not as same as the class in Java and C++ , will work for what you want in this case.

In your case this is how your code will look.

class Animal a where 
say :: String -> sound 

Then you can have individual data types adapting these methods.

instance Animal Dog where
say s = "bark " ++ s 

EDIT :- Before you can specialize say for Dog you need to tell the system that Dog is animal.

data Dog =  \--something here --\ (deriving animal)

EDIT :- For Wilq.
Now if you want to use say in a function say foo, you will have to tell haskell that foo can only work with Animal.


foo :: (Animal a) => a -> String -> String
foo a str = say a str 

now if you call foo with dog it will bark, if you call with cat it will meow.


main = do 
let d = dog (\--cstr parameters--\)
    c = cat  
in show $ foo d "Hello World"

You now can not have any other definition of function say. If say is called with anything not animal will cause compile error.

edited body
Source Link
Manoj R
  • 4.1k
  • 25
  • 30

In Haskell, the closest you have is "class". This class though not as same as the class in Java and C++ , will work for what you want in this case.

In your case this is how your code will look.

class Animal a where 
say :: String -> sound 

Then you can have individual data types adapting these methods.

instance animalAnimal Dog where
say s = "bark " ++ s 

EDIT :- Before you can specialize say for Dog you need to tell the system that Dog is animal.

data Dog =  \--something here --\ (deriving animal)

In Haskell, the closest you have is "class". This class though not as same as the class in Java and C++ , will work for what you want in this case.

In your case this is how your code will look.

class Animal a where 
say :: String -> sound 

Then you can have individual data types adapting these methods.

instance animal Dog where
say s = "bark " ++ s 

EDIT :- Before you can specialize say for Dog you need to tell the system that Dog is animal.

data Dog =  \--something here --\ (deriving animal)

In Haskell, the closest you have is "class". This class though not as same as the class in Java and C++ , will work for what you want in this case.

In your case this is how your code will look.

class Animal a where 
say :: String -> sound 

Then you can have individual data types adapting these methods.

instance Animal Dog where
say s = "bark " ++ s 

EDIT :- Before you can specialize say for Dog you need to tell the system that Dog is animal.

data Dog =  \--something here --\ (deriving animal)
added 163 characters in body
Source Link
Manoj R
  • 4.1k
  • 25
  • 30
Loading
sorry, but Java is not an acronym ;-)
Source Link
Joachim Sauer
  • 11k
  • 3
  • 55
  • 45
Loading
added 15 characters in body
Source Link
Manoj R
  • 4.1k
  • 25
  • 30
Loading
Source Link
Manoj R
  • 4.1k
  • 25
  • 30
Loading