Created
December 8, 2017 22:51
-
-
Save boj/a4a852b7e79cc5cc5bc62f92cf0567cf to your computer and use it in GitHub Desktop.
Haskell ViewPatterns example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env nix-shell | |
| #! nix-shell -i 'runghc -XViewPatterns' -p 'ghc.withPackages (pkgs: [ pkgs.containers ])' | |
| module Main where | |
| import Data.Map (Map) | |
| import qualified Data.Map as Map | |
| f :: Int -> Map Int String -> String | |
| f k (Map.lookup k -> Just s) = s | |
| f _ _ = "" | |
| main = do | |
| let m = Map.fromList [(0, "a"), (1, "b")] | |
| print $ f 0 m | |
| print $ f 1 m | |
| print $ f 2 m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment