Skip to content

Instantly share code, notes, and snippets.

@nattybear
Last active December 29, 2020 23:17
Show Gist options
  • Save nattybear/630fc2a4f419b23ccc297d9eff2b8eba to your computer and use it in GitHub Desktop.
Save nattybear/630fc2a4f419b23ccc297d9eff2b8eba to your computer and use it in GitHub Desktop.
하스켈 infix 연산자

이 글은 하스켈 위키를 읽고 정리한 것이다.

하스켈에서 함수를 사용할 때는 보통 함수 이름을 먼저 적고 인자를 나중에 적는다. prefix notation

그런데 + 같은 함수는 두 인자 사이에 함수 이름을 적는다. infix notation

infix 함수를 prefix로 사용하기

infix 함수를 괄호로 감싸면 prefix처럼 사용할 수 있다.

ghci> (+) 1 2
3
ghci> (*) 3 4
12

prefix 함수를 infix로 사용하기

prefix 함수를 ` 로 감싸면 infix 처럼 사용할 수 있다.

ghci> let concatPrint x y = putStrLn $ (++) x y
ghci> concatPrint "a" "b"
ab
ghci> "a" `concatPrint` "b"
ab

여기서 설명한 것은 인자의 개수가 두개인 함수에만 적용하는 게 좋다. 인자가 두개 이상일 경우에도 할 수는 있는데 별로 나이스 하지는 않다.

ghci> foldl (+) 0 [1..5]
15
ghci> ((+) `foldl` 0) [1..5]
15

대문 링크

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment