Introduction to Haskell

Posted on 2015-08-15 22:16:12 +0900 in Functional Programming Haskell CIS194

Lecture Content

Among all of them, Pure is the most tricky part. In the current computer system model, side effects are inevitable. Haskell uses Modad and treats the outside world as static to implement Pure.

Home Work

Exercise 1
toDigits :: Integer -> [Integer]
toDigits n
  | n <= 0 = []
  | otherwise = toDigits (n `div` 10) ++ [n `mod` 10]

toDigitsRev :: Integer -> [Integer]
toDigitsRev = reverse . toDigits

Test part,

ex1Tests :: [Test]
ex1Tests = [ testF1 "toDigits test" toDigits
             [(1234, [1, 2, 3, 4]), (5, [5]), (0, []), (-17, [])]
           ]
Exercise 2

The key lies in generating infinite lists of [1, 2, 1, 2, ...] There are several ways to generate infinite lists.

numbers1 = 1 : map (+1) numbers1
numbers2 = 1 : [x+1 | x <- numbers2]
numbers3 = [1..]

The way to get infinite interchange of 1 2 lists is

  oneTwo = 1 : 2 : oneTwo
doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther = reverse . zipWith (*) oneTwo . reverse where
  oneTwo = 1 : 2 : oneTwo

Test code

ex2Tests :: [Test]
ex2Tests = [testF1 "doubleEveryOther test" doubleEveryOther
             [([8, 7, 6, 5], [16, 7, 12, 5]), ([1, 2, 3], [1, 4, 3])]
           ]
Exercise 3
sumDigits :: [Integer] -> Integer
sumDigits = sum . map (sum. toDigits)
ex3Tests :: [Test]
ex3Tests = [testF1 "sumDigits test" sumDigits
             [([16, 7, 12, 5], 22)]
           ]
Exercise 4
validate :: Integer -> Bool
validate = (== 0) . (`mod` 10) . sumDigits . doubleEveryOther . toDigits
ex4Tests :: [Test]
ex4Tests = [testF1 "validate test" validate
             [(4012888888881881, True), (4012888888881882, False)]
           ]
Exercise 5
type Peg = String
type Move = (Peg, Peg)

hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi n a b c
  | n <= 0 = []
  | n == 1 = [(a, b)]
  | otherwise = hanoi (n - 1) a c b ++ [(a, b)] ++ hanoi (n - 1) c b a
----------------------------------- 本文内容遵从CC版权协议转载请注明出自kamelzcs -----------------------------------
«  | Learn Haskell »

Hide Comments

comments powered by Disqus