I am learning haskell and write for exercise following function:
cipherCeasar :: Char -> Int -> Char
cipherCeasar c i
| isUpper c = chr (isBiggerMax ((+) i $ ord c) (ord 'A') (ord 'Z'))
| otherwise = chr (isBiggerMax ((+) i $ ord c) (ord 'a') (ord 'z'))
As you can see the two lines of codes after guard are almost the same.
How can I generalize code or it is ok so?
You can use a helper function:
cipherCeasar :: Char -> Int -> Char
cipherCeasar c i
| isUpper c = cipherBetween 'A' 'Z'
| otherwise = cipherBetween 'a' 'z'
where cipherBetween min max = chr (isBiggerMax ((+) i $ ord c) (ord min) (ord max))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.