Polymorphism in haskell - using multiple versions of the same function without giving it different names

the other day I wrote a small program to collect a bunch of numbers in a matrix - data Matrix = Matrix [[Int]]starting from an angle - Cornerand following Path - [Direction], all three types are instances of the classTransformable

then I have a function that generates a transform function that turns a given angle and direction into LeftUpandRight

turnLUR :: Transformable a => (Corner, Direction) -> a -> a

then I use this function to "collect" all the numbers in the matrix:

harvest :: Matrix → [(Corner,Direction)] → [Int]
harvest _ [] = []
harvest (Matrix []) _ = []
harvest as (cd:csds) = b ++ harvest (Matrix bs) csds'  --cd = (Corner,Direction)
                     where f1 = turnLUR cd -- Matrix -> Matrix
                           f2 = turnLUR cd -- Corner -> Corner
                           f3 = turnLUR cd -- Direction -> Direction
                           Matrix (b:bs) = f1 as -- b = first line of [[Int]]
                           fcfd (c,d) = (f2 c,f3 d)
                           csds' = map fcfd csds

Now my question is, why do I have to write f1, f2and f3, instead of a single function fthree times (keeping in mind DRY!) - all three types Corners, Directionsand Matrixare instances class Transformable.

" " ?

+5
1

. , :

harvest :: Matrix → [(Corner,Direction)] → [Int]
harvest _ [] = []
harvest (Matrix []) _ = []
harvest as (cd:csds) = b ++ harvest (Matrix bs) csds'  --cd = (Corner,Direction)
                     where f :: Transformable a => a -> a
                           f = turnLUR cd
                           Matrix (b:bs) = f as -- b = first line of [[Int]]
                           fcfd (c,d) = (f c,f d)
                           csds' = map fcfd csds

, {-# LANGUAGE NoMonomorphismRestriction #-} .

- GHC - - , (. wiki, ), . .

+11

All Articles