Created
March 5, 2013 21:45
-
-
Save exterm/5094584 to your computer and use it in GitHub Desktop.
Translating Python to Haskell, keeping core concepts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A simple class in Python | |
| class car(): | |
| # The __init__ method | |
| def __init__(self, color): | |
| self.color = color | |
| # The color method | |
| def color(self): | |
| return self.color | |
| # The mpg method | |
| def mpg(self): | |
| return 45 | |
| # Construct a car object then call the mpg method. | |
| if __name__ == '__main__': | |
| c = car('red') | |
| # Note that car.mpg(c) is the same as c.mpg() | |
| print car.mpg(c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- A simple class in Haskell | |
| data Car = Car String | |
| -- Function for constructing a new car | |
| -- This takes the place of the __init__ method | |
| car color = Car color | |
| -- The color function | |
| color (Car color) = color | |
| -- The mpg function | |
| mpg (Car _) = 45 | |
| -- Construct a car object then call the mpg method. | |
| main = | |
| let c = car "red" | |
| in print (mpg c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A simple class in Python | |
| class car(): | |
| # The __init__ method | |
| def __init__(self, color): | |
| self.color = color | |
| # The color method | |
| def color(self): | |
| return self.color | |
| # The mpg method | |
| def mpg(self): | |
| return 45 | |
| # Basic inheritance | |
| class truck(car): | |
| # The overridden mpg method | |
| def mpg(self): | |
| return 14 | |
| # Construct a truck object then call the mpg method. | |
| if __name__ == '__main__': | |
| t = truck('red') | |
| print truck.mpg(t) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- A simple class in Haskell | |
| data Car = Car String | |
| | Truck String -- Basic inheritance | |
| -- Function for constructing a new car | |
| -- This takes the place of the __init__ method | |
| car color = Car color | |
| -- The color function | |
| color (Car color) = color | |
| -- The mpg function | |
| -- Both the truck and car variants are handled here | |
| mpg c = case c of | |
| (Car _) -> 45 | |
| (Truck _) -> 14 | |
| -- Function for constructing a new truck | |
| -- This takes the place of the __init__ method | |
| truck color = Truck color | |
| -- Construct a car object then call the mpg method. | |
| main = | |
| let t = truck "red" | |
| in print (mpg t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment