The document describes a Haskell program that translates characters in one string to characters in another string. It defines a translate function that maps characters from the first string (set1) to the corresponding characters in the second string (set2). A translateString function applies the translate function to a given string, and the main function gets the set1 and set2 strings from arguments, reads stdin, applies translateString, and writes the result to stdout, catching any errors.
19. What's faster than C++, more concise than Perl,
more regular than Python, more flexible than Ruby,
more typeful than C#, more robust than Java,
and has absolutely nothing in common with PHP?
It's Haskell!
-- Autrijus Tang( )
C++ Perl
Python Ruby
C# Java
PHP
Haskell
20.
21. 1 cond :: Bool
2 cond = True
3
4 price :: Int
5 price = 2800
6
7 pi :: Float
8 pi = 3.141592653589793
9
10 greeting :: String
11 greeting = "Hello, Haskell!"
12
13 letter :: Char
14 letter = 'C'
24. 1 double :: Int -> Int
2 double x = x + x
3
4 add :: (Int, Int) -> Int
5 add (x, y) = x + y
6
7 mult :: Int -> Int -> Int -> Int
8 mult x y z = x * y * z
> double 3
6
> add (1, 7)
8
> mult 7 8 9
504
25. 1 add :: (Int, Int) -> Int
2 add (x, y) = x + y
3
4 -- Int -> (Int -> Int)
Haskell
5 add' :: Int -> Int -> Int
6 add' x y = x + y 1
7
8 mult10 :: Int -> Int -> Int
9 mult10 = mult 10
10
11 mult30 :: Int -> Int
12 mult30 = mult 10 3
> mult10 3 8
240
> mult30 5
150
26. 1 twice :: (Int -> Int) -> Int -> Int
2 twice f x = f (f x)
3 Int
4 isDigit :: Char -> Bool
Int
5 isDigit c = c >= '0' && c <= '9'
6
7 -- map :: (a -> b) -> [a] -> [b]
> twice (*2) 3
12
> map (+1) [1, 2, 3, 4]
[2, 3, 4, 5]
> map isDigit ['a', '0', 'b', '9']
[False, True, False, True]
28. > 1 + 2
3
> (*) 1.1 2.3 -- 1.1 * 2.3
2.53
> 10 `div` 2 -- div 10 2
5
1 (+) :: Num a => a -> a -> a
2 (*) :: Num a => a -> a -> a
3 negate :: Num a => a -> a
4 abs :: Num a => a -> a
29. 1 -- ( Ord, Show, Read )
2 class Eq a where
3 (==) :: a -> a -> Bool
4 (/=) :: a -> a -> Bool
5
6 -- MyType Eq
7 instance Eq MyType where
8 (MyType a) == (MyType a') = (a == a')
30. 1 -- [λxy. x+y]
2 add x y = x + y
3 add' = x -> (y -> x + y)
4 add'' = x y -> x + y
5
6 --
7 -- const x y = x
8 const :: a -> (b -> a)
9 const x = _ -> x
10
11 --
> map (x -> x * 2 + 1) [1, 3, 5, 7]
[3, 7, 11, 15]
31. > [x^2 | x <- [1..5]]
[1,4,9,16,25]
> [(x, y) | x <- [1,2,3], y <- [4,5]]
[(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)]
1 --
2 factors :: Int -> [Int]
3 factors n = [x | x <- [1..n], n `mod` x == 0]
4 --
5 primes :: Int -> [Int]
6 primes n = [x | x <- [2..n], factors x == [1, x]]
> factors 100
[1,2,4,5,10,20,25,50,100]
> primes 50
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
32. 1 --
2 factorial :: Int -> Int
3 factorial 0 = 1
4 factorial n = n * factorial (n - 1)
5
6 --
7 product :: Num a => [a] -> a
8 product [] = 1
9 product (n:ns) = n * product ns
1 2 3 4
1:[2,3,4] 4:[]
39. 1 ones :: [Int]
2 ones = 1 : ones
3
4 -- ( )
5 sieve :: [Int] -> [Int]
6 sieve (p:xs) = p : sieve [x|x <- xs, x `mod` p /= 0]
7
8 primes :: [Int]
9 primes = sieve [2..] head ones
= {head ones }
> head ones head (1:ones)
1 = {head }
> take 10 primes 1
[2,3,5,7,11,13,17,19,23,29]
40. Mac OS X 10.7.1
tarai :: Int -> Int -> Int -> Int
1.8 GHz Intel Core i7
tarai x y z
4 GB 1333 MHz DDR3
| x <= y = y
| otherwise = tarai (tarai (x-1) y z)
> tarai 16 10 0
(tarai (y-1) z x)
16
(tarai (z-1) x y)
Haskell
private int tarai(int x, int y, int z) {
:107039ms
if (x <= y) {
return y; :45,722,185,221
} else {
return tarai(tarai(x-1, y, z),
tarai(y-1, z, x),
tarai(z-1, x, y));
}
} Java
47. 6 -- set1 set2
7 translate :: String -> String -> Char -> Char
8 translate [] _ c = c
9 translate (x:xs) [] c = if x == c then ' ' else translate xs [] c
10 translate (x:xs) [y] c = if x == c then y else translate xs [y] c
11 translate (x:xs) (y:ys) c = if x == c then y else translate xs ys c
12
13 --
14 translateString :: String -> String -> String -> String
15 translateString set1 set2 str = map (translate set1 set2) str
16
17 usage :: IOError -> IO ()
18 usage e = do putStrLn "Usage: ex14 set1 set2"
19 putStrLn "Translates characters in set1 on stdin to the corresponding"
20 putStrLn "characters from set2 and writes the translation to stdout."
21
22 --
23 main :: IO ()
24 main = (do [set1,set2] <- getArgs
25 contents <- hGetContents stdin
26 putStr $ translateString set1 set2 contents) `catchError` usage
48. 6 -- set1 set2
7 translate :: String -> String -> Char -> Char
8 translate [] _ c = c
9 translate (x:xs) [] c = if x == c then ' ' else translate xs [] c
10 translate (x:xs) [y] c = if x == c then y else translate xs [y] c
11 translate (x:xs) (y:ys) c = if x == c then y else translate xs ys c
12
13 --
14 translateString :: String -> String -> String -> String
15 translateString set1 set2 str = map (translate set1 set2) str
16
17 usage :: IOError -> IO ()
18 usage e = do putStrLn "Usage: ex14 set1 set2"
19 putStrLn "Translates characters in set1 on stdin to the corresponding"
20 putStrLn "characters from set2 and writes the translation to stdout."
21
22 --
23 main :: IO ()
24 main = (do [set1,set2] <- getArgs
25 contents <- hGetContents stdin
26 putStr $ translateString set1 set2 contents) `catchError` usage
50. 1 class Monad m where
2 (>>=) :: m a -> (a -> m b) -> m b
3 return :: a -> m a
4
5 -- data Bool = False | True
6 data Maybe a = Nothing | Just a
7
8 instance Monad Maybe where
9 return = Just ( )
10 fail = Nothing
11 Nothing >>= f = Nothing Just Nothing
12 (Just x) >>= f = f x 1
Maybe Int
51. lookup :: Eq a => a -> [(a, b)] -> Maybe b
players = [(6, "Nakata"), (11, "Darvish"), (41, "Inaba")]
> lookup 11 players
Just "Darvish"
> lookup 212 players
Nothing
52. 1 pacific = [("Fs", [(6, "Nakata"), (11, "Darvish"), (41, "Inaba")]),
2 ("SH", [(3, "Matsunaka"), (21, "Wada"), (47, "Sugiuchi")]),
3 ("Es", [(18, "Tanaka"), (21, "Iwakuma"), (32, "Matsui")])]
4
5 lookupName :: String -> Int -> Maybe String
6 lookupName t n = case (lookup t pacific) of
7 Just players -> lookup n players
8 Nothing -> Nothing
> lookupName "Fs" 41
Just "Inaba"
…
53. -- ( )
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
instance Monad Maybe where
return = Just
fail = Nothing
Nothing >>= f = Nothing
(Just x) >>= f = f x
Nothing Maybe Nothing
>>= Int -> ? =
Just Maybe Maybe
Int >>= Int -> ? = ?
54. 1 pacific = [("Fs", [(6, "Nakata"), (11, "Darvish"), (41, "Inaba")]),
2 ("SH", [(3, "Matsunaka"), (21, "Wada"), (47, "Sugiuchi")]),
3 ("Es", [(18, "Tanaka"), (21, "Iwakuma"), (32, "Matsui")])]
4
5 lookupName :: String -> Int -> Maybe String
6 lookupName t n = case (lookup t pacific) of
7 Just players -> lookup n players
8 Nothing -> Nothing
9
10 lookupName' t n = lookup t pacific >>= lookup n
> lookupName' "Es" 18
Just "Tanaka" [(Int, b)] -> Maybe b
> lookupName' "Fs" 18
Nothing Maybe [(Int, String)]
> lookupName' "DH" 21
Nothing
--
(>>=) :: m a -> (a -> m b) -> m b
lookup :: Eq a => a -> [(a, b)] -> Maybe b
55. Maybe Maybe Maybe Mayb
a >>= a -> b >>= b -> c >>= c -> d
56. IO() String -> IO()
main = putStrLn "Hello, World!"
main = let a = putStrLn "Hello" in (a >> a)
IO a