Difference between revisions of "Implicit parameters"

From HaskellWiki
Jump to navigation Jump to search
(link to ghc manual)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
The GHC manual on implicit parameters: [http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#implicit-parameters].
+
The GHC manual on implicit parameters: [https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#implicit-parameters].
  +
  +
Working example:
  +
  +
{-# LANGUAGE ImplicitParams #-}
  +
  +
import Data.List (sortBy)
  +
  +
sortBy' :: (?cmp :: a -> a -> Ordering) => [a] -> [a]
  +
sortBy' = sortBy ?cmp
  +
  +
sort :: Ord a => [a] -> [a]
  +
sort = let ?cmp = compare in sortBy'
  +
  +
main = putStrLn (show (sort [3,1,2]))
   
 
[[Category:Language extensions]]
 
[[Category:Language extensions]]

Revision as of 14:19, 9 July 2016

The GHC manual on implicit parameters: [1].

Working example:

{-# LANGUAGE ImplicitParams #-}

import Data.List (sortBy)

sortBy' :: (?cmp :: a -> a -> Ordering) => [a] -> [a]
sortBy' = sortBy ?cmp
sort :: Ord a => [a] -> [a]
sort = let ?cmp = compare in sortBy'

main = putStrLn (show (sort [3,1,2]))