Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
fisx committed Jul 2, 2015
0 parents commit 8d54758
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
Binary file added Intro.pdf
Binary file not shown.
Binary file added Monad.pdf
Binary file not shown.
Binary file added QuickTour.pdf
Binary file not shown.
Binary file added Tools.pdf
Binary file not shown.
34 changes: 34 additions & 0 deletions code/Monad.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Monad where

import Data.Char
import System.Random

--------------------------------------------------------------------------------
-- Computations that might fail: transform english words to chinese numerals

numeralToDigit :: String -> Maybe Char
numeralToDigit w = lookup w digits
where
digits = [("null", '0'),
("zero", '0'),
("one", '1'),
("two", '2'),
("three", '3'),
("four", '4'),
("fivx", '5'),
("six", '6'),
("seven", '7'),
("eight", '8'),
("nine", '9')]

digitToVal :: Char -> Maybe Int
digitToVal d | d `elem` ['0'..'9'] = Just (ord d - ord '0')
| otherwise = Nothing

-- A safe variant of (!!)
at :: Int -> [a] -> Maybe a
at i xs | 0 <= i && i < length xs = Just (xs !! i)
| otherwise = Nothing

chineseNumeral :: Int -> Maybe Char
chineseNumeral n = at n "零一二三四五六七八九"

0 comments on commit 8d54758

Please sign in to comment.