http://www.haskell.org/pipermail/haskell-cafe/2007-August/030096.html에서 typeclass 메서드 collide
은 2 개의 "일반"인수가 아닌 하나의 인수로 2- 튜플을 취하는 것으로 정의됩니다 (부분 적용 등을 이해할 수 있습니다).Haskell 함수는 언제 다중 인수가 아닌 튜플을 취해야합니까?
{-# OPTIONS_GHC -fglasgow-exts
-fallow-undecidable-instances
-fallow-overlapping-instances #-}
module Collide where
class Collide a b where
collide :: (a,b) -> String
data Solid = Solid
data Asteroid = Asteroid
data Planet = Planet
data Jupiter = Jupiter
data Earth = Earth
instance Collide Asteroid Planet where
collide (Asteroid, Planet) = "an asteroid hit a planet"
instance Collide Asteroid Earth where
collide (Asteroid, Earth) = "the end of the dinos"
-- Needs overlapping and undecidable instances
instance Collide a b => Collide b a where
collide (a,b) = collide (b, a)
-- ghci output
*Collide> collide (Asteroid, Earth)
"the end of the dinos"
*Collide> collide (Earth, Asteroid)
"the end of the dinos"
이 기능의 목적은 무엇입니까?
다중 인수가 아닌 튜플 인수를 사용하는 것이 더 좋은 경우는 언제입니까?
http://programmers.stackexchange.com/questions/185585/what-is-the-advantage-of-currying – Sibi
@Sibi는 하스켈의 자동 작업이 필요하지 않는 한 항상 튜플을 사용하도록 권장합니다. curreying/uncurrying? – fadedbee
내 _opinion_은 좌표가'(x, y) '인 튜플처럼 두 값이 본질적으로 연결된 경우 튜플을 사용해야한다는 것입니다. 다행히 2 튜플의 경우, 우리는 다른 하나보다 편리 할 때마다이 표현들 사이를 변환하는'카레'와'uncurry '를 가지고 있습니다. – bheklilr