Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell "Dot Hack"

Tags:

haskell

In the Wiki page for Sudoku solutions, one solution claims to use the "Dot Hack". The linked Github page is no more available and I couldn't find anything about it elewhere.

What is this about? What does it do? How?

like image 538
fabb Avatar asked Jun 29 '12 08:06

fabb


2 Answers

I guess he's referring to the following line:

import Prelude hiding ((.))

which disables the normal (.) operator for functional composition. Instead, another operator with the same name is used, probably imported from the utility module T.T. This operator behaves like in OOP languages:

pretty_output solution = solution.elems.map(show).in_group_of(9)
    .map(unwords).unlines

which (I think) would normally look like

pretty_output solution = (unlines . map unwords . in_group_of 9 . map show . elems) solution

That operator works the same like the |> operator in F#:

(|>) :: a -> (a -> b) -> b
x |> f = f x

which is used to pipe a value through functions (and is more readable and better functional style, imo):

pretty_output solution = solution |> elems |> map show |> in_group_of 9 |> map unwords |> unlines

(|>) is also the same as flip ($).

Edit: This "hacked" operator already exists in Haskell, somehow. The same composition behavior can be achieved by the left-to-right composition operator from Control.Category:

g x = x |> (f1 >>> f2 >>> f3)

This pipes only functions, though, and is actually just f >>> g = g . f.

like image 170
phipsgabler Avatar answered Nov 14 '22 09:11

phipsgabler


It's using the OOP style

thing.method

to call functions on thing instead of the usual

method thing

See for example

row i = i `div` 9
col i = i `mod` 9
row_list i positions = positions.select(on_i_row) where
  on_i_row pos = pos.row  == i.row
col_list i positions = positions.select(on_i_col) where
  on_i_col pos = pos.col == i.col

in that programme.

like image 4
Daniel Fischer Avatar answered Nov 14 '22 10:11

Daniel Fischer