-
Notifications
You must be signed in to change notification settings - Fork 0
/
Normal_Recursion.hs
135 lines (107 loc) · 3.09 KB
/
Normal_Recursion.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
{- | CSE 130: Intro to Haskell Assignment.
Do not change the skeleton code!
You may only replace the `error "TBD:..."` parts.
For this assignment, you may use any library function on integers
but only the following library functions on lists:
length
(++)
(==)
-}
module Hw1 where
import Prelude hiding (replicate, sum, reverse)
-- | Sum the elements of a list
--
-- >>> sumList [1, 2, 3, 4]
-- 10
--
-- >>> sumList [1, -2, 3, 5]
-- 7
--
-- >>> sumList [1, 3, 5, 7, 9, 11]
-- 36
sumList :: [Int] -> Int
sumList [] = 0
sumList (x:xs) = x + sumList xs
-- | `digitsOfInt n` should return `[]` if `n` is not positive,
-- and otherwise returns the list of digits of `n` in the
-- order in which they appear in `n`.
--
-- >>> digitsOfInt 3124
-- [3, 1, 2, 4]
--
-- >>> digitsOfInt 352663
-- [3, 5, 2, 6, 6, 3]
digitsOfInt :: Int -> [Int]
digitsOfInt (n)
| n < 10 = [n]
| otherwise = digitsOfInt(n `div` 10) ++[ (n `mod` 10) ]
-- | `digits n` retruns the list of digits of `n`
--
-- >>> digits 31243
-- [3,1,2,4,3]
--
-- digits (-23422)
-- [2, 3, 4, 2, 2]
digits :: Int -> [Int]
digits n = digitsOfInt (abs n)
-- | From http://mathworld.wolfram.com/AdditivePersistence.html
-- Consider the process of taking a number, adding its digits,
-- then adding the digits of the number derived from it, etc.,
-- until the remaining number has only one digit.
-- The number of additions required to obtain a single digit
-- from a number n is called the additive persistence of n,
-- and the digit obtained is called the digital root of n.
-- For example, the sequence obtained from the starting number
-- 9876 is (9876, 30, 3), so 9876 has
-- an additive persistence of 2 and
-- a digital root of 3.
--
-- NOTE: assume additivePersistence & digitalRoot are only called with positive numbers
-- >>> additivePersistence 9876
-- 2
additivePersistence :: Int -> Int
additivePersistence n
| n < 10 = 0
| otherwise = (additivePersistence(sumList(digits(n)))) + 1
-- | digitalRoot n is the digit obtained at the end of the sequence
-- computing the additivePersistence
--
-- >>> digitalRoot 9876
-- 3
digitalRoot :: Int -> Int
digitalRoot n
| n < 10 = n
| otherwise = (digitalRoot(sumList(digits(n))))
-- | listReverse [x1,x2,...,xn] returns [xn,...,x2,x1]
--
-- >>> listReverse []
-- []
--
-- >>> listReverse [1,2,3,4]
-- [4,3,2,1]
--
-- >>> listReverse ["i", "want", "to", "ride", "my", "bicycle"]
-- ["bicycle", "my", "ride", "to", "want", "i"]
-- sumList :: [Int] -> Int
-- sumList [] = 0
-- sumList (x:xs) = x + sumList xs
listReverse :: [a] -> [a]
listReverse [] = []
listReverse (x:xs) = listReverse xs ++ [x]
-- | In Haskell, a `String` is a simply a list of `Char`, that is:
--
-- >>> ['h', 'a', 's', 'k', 'e', 'l', 'l']
-- "haskell"
--
-- >>> palindrome "malayalam"
-- True
--
-- >>> palindrome "myxomatosis"
-- False
getHead (x:xs) = x
getTails (x:xs) = xs
palindrome :: String -> Bool
palindrome w
| length w < 2 = True
| (getHead w) /= (getHead(listReverse(w))) = False
| otherwise = palindrome( getTails( listReverse( getTails(w) ) ) )