I am new to Haskell and I am trying to understand a game created in Haskell (tic tac toe). I know that if a function takes n parameters then you must provide n parameters in the function definition. Example:
f :: Int -> Int -> String
f a b = "This function makes no sense"
However in this Haskell script there is a function that takes two arguments but in the definition it has none. And of course, it's working but I can't seem to figure out why.
import Data.Map qualified as M
type Board = M.Map (Int, Int) Marker
data Marker = X | O | Blank deriving Eq
getMarker :: Board -> (Int, Int) -> Marker
getMarker = flip $ M.findWithDefault Blank
Any ideas on what this function does and more importantly, why it's working (you can see that getMarker
takes 0 parameters at the last line) ?
The misconception is here:
... (you can see that
getMarker
takes 0 parameters at the last line) ...
and the thing that's puzzling you is partial application.
getMarker :: Board -> (Int, Int) -> Marker
getMarker = flip $ M.findWithDefault Blank
What that last line actually tells you is that the getMarker
doesn't do anything with its arguments - but they still get passed to the function created by flip $ M.findWithDefault Blank
.
Or, more accurately, getMarker
evaluates to a function of the declared type which is applied to getMarker
's arguments.