Difference between revisions of "Implicit parameters"

From HaskellWiki
Jump to navigation Jump to search
m (Fix link to GHC doc)
(One intermediate revision by one other user not shown)
Line 1: Line 1:
The GHC manual on implicit parameters: [https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#implicit-parameters].
+
{{GHCUsersGuide|https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#implicit-parameters Implicit Parameters section}}
   
 
Working example:
 
Working example:
Line 7: Line 7:
 
import Data.List (sortBy)
 
import Data.List (sortBy)
 
 
sort :: (?cmp :: a -> a -> Ordering) => [a] -> [a]
+
sortBy' :: (?cmp :: a -> a -> Ordering) => [a] -> [a]
sort = sortBy ?cmp
+
sortBy' = sortBy ?cmp
  +
  +
sort :: Ord a => [a] -> [a]
  +
sort = let ?cmp = compare in sortBy'
 
 
main = let ?cmp = compare in putStrLn (show (sort [3,1,2]))
+
main = putStrLn (show (sort [3,1,2]))
   
 
[[Category:Language extensions]]
 
[[Category:Language extensions]]

Revision as of 03:25, 27 October 2016

The GHC Users Guide has Implicit Parameters section.html#{{{2}}} {{{3}}}.

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