#3 [wip] Add fbo answers for homework 1
Opened 4 years ago by fbo. Modified 4 years ago
fbo/cis194 fbo-homework1  into  master

@@ -0,0 +1,11 @@ 

+ toDigits :: Integer -> [Integer]

+ toDigits r

+   | r <= 0 = []

+   | otherwise = toDigits (div r 10) ++ [mod r 10]

+ 

+ toDigitsRev :: Integer -> [Integer]

+ toDigitsRev r

+   | r <= 0 = []

+   | otherwise = (mod r 10) : toDigitsRev (div r 10)

+ 

+ main = print (toDigits (-17))

nicely done!

@@ -0,0 +1,9 @@ 

+ doubleEveryOtherLR :: [Integer] -> [Integer]

+ doubleEveryOtherLR [] = []

+ doubleEveryOtherLR (x:[]) = [x]

+ doubleEveryOtherLR (x:y:zs) = x : y*2 : doubleEveryOtherLR zs

+ 

+ doubleEveryOther :: [Integer] -> [Integer]

+ doubleEveryOther zs = reverse (doubleEveryOtherLR (reverse zs))

+ 

+ main = print (doubleEveryOther [10, 9, 8, 7])

@@ -0,0 +1,9 @@ 

+ intToList :: Integer -> [Integer]

+ intToList 0 = []

+ intToList x = (mod x 10) : (intToList (div x 10))

+ 

+ sumDigits :: [Integer] -> Integer

+ sumDigits [] = 0

+ sumDigits (x:xs) = sum(intToList x) + sumDigits xs

+ 

+ main = print (sumDigits [12, 4])

@@ -0,0 +1,31 @@ 

+ toDigits :: Integer -> [Integer]

+ toDigits r

+   | r <= 0 = []

+   | otherwise = toDigits (div r 10) ++ [mod r 10]

+ 

+ doubleEveryOtherLR :: [Integer] -> [Integer]

+ doubleEveryOtherLR [] = []

+ doubleEveryOtherLR (x:[]) = [x]

+ doubleEveryOtherLR (x:y:zs) = x : y*2 : doubleEveryOtherLR zs

+ 

+ doubleEveryOther :: [Integer] -> [Integer]

+ doubleEveryOther zs = reverse (doubleEveryOtherLR (reverse zs))

+ 

+ intToList :: Integer -> [Integer]

+ intToList 0 = []

+ intToList x = (mod x 10) : (intToList (div x 10))

+ 

+ sumDigits :: [Integer] -> Integer

+ sumDigits [] = 0

+ sumDigits (x:xs) = sum(intToList x) + sumDigits xs

+ 

+ validate :: Integer -> Bool

+ validate x = 

+   if mod (

+     sumDigits (

+       doubleEveryOther (toDigits x))) 10 == 0

+   then True

+   else False

+ 

"if x then True else False" can be written as "x" when x evaluate as a Bool ;)

validate x = mod ( sumDigits (doubleEveryOther (toDigits x))) 10 == 0

+ main = print (validate 4012888888881881)

+ main = print (validate 4012888888881882)

@@ -0,0 +1,12 @@ 

+ type Peg = String

+ type Move = (Peg, Peg)

+ hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]

+ hanoi n a b c

+   | n == 1 = [(a, c) :: Move]

+ 

+ a = "A" :: Peg

+ b = "B" :: Peg

+ c = "C" :: Peg

+ 

"type x = y" actually just creates an alias for "y" named "x". Thus Peg is a String, and "a" is already a String. That means you can call (hanois 1 "a" "b" "c") directly.

+ main = print (hanoi 1 a b c)

+ -- TBC

no initial comment

Build failed.

1 new commit added

  • Add ex3
4 years ago

Build failed.

1 new commit added

  • Provided ex4 and started ex5
4 years ago

Build failed.

"if x then True else False" can be written as "x" when x evaluate as a Bool ;)

validate x = mod ( sumDigits (doubleEveryOther (toDigits x))) 10 == 0

"type x = y" actually just creates an alias for "y" named "x". Thus Peg is a String, and "a" is already a String. That means you can call (hanois 1 "a" "b" "c") directly.