Personal tools

Implicit parameters

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(link to ghc manual)
 
 
(5 intermediate revisions by 2 users 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].
+
{{GHCUsersGuide|glasgow_exts|implicit-parameters|an Implicit Parameters section}}
 +
 
 +
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]]
 
[[Category:GHC]]
 
[[Category:GHC]]
 
[[Category:Stub articles]]
 
[[Category:Stub articles]]

Latest revision as of 00:28, 14 November 2016

The GHC Users Guide has an Implicit Parameters section.

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]))