2013년 2월 11일 월요일

const & id

const id

const x y = x
const id x y = y
const id <=> flip const
      by H-99: 99 Haskell Problems Solutions

생각

const id x = id이므로,
const id x y
 <=> id y
 <=> y

다른 방법

id :: a -> a 이고
const :: b -> c -> b 이므로,
const id :: (a -> a) -> c -> (a -> a)
그러므로, id를 pass through하고 나면,
c -> (a -> a). 즉,  c -> a -> a
결론적으로, 두번째 요소가 반환된다. (맞나?)


[ from 山本和彦 ]

정의
id :: a -> a
id x = x

const :: a -> b -> a
const x _ = x

id의 사용
  • 함수를 fold할 때의 초기값.
map (+1) . filter odd . take 10
<=> foldr (.) id [map (+1), filter odd, take 10]
  • Bool을 그대로 되돌려주는 술어.
takeWhile (==True) [True,True,True,False,True]
<=> takeWhile id [True,True,True,False,True]
const의 사용
  • 인수를 무시하는 함수.
ignore :: SomeException -> IO ()
ignore = const (return ())
-- ignore _ = return ()

댓글 없음:

댓글 쓰기