Skip to content

Instantly share code, notes, and snippets.

@boj
Created December 8, 2017 22:51
Show Gist options
  • Select an option

  • Save boj/a4a852b7e79cc5cc5bc62f92cf0567cf to your computer and use it in GitHub Desktop.

Select an option

Save boj/a4a852b7e79cc5cc5bc62f92cf0567cf to your computer and use it in GitHub Desktop.
Haskell ViewPatterns example
#! /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