使用哈士奇语言实现Caesar ciphers

module Cipher where
    import Data.Char
    data Shift = 
        LeftShift Integer | RightShift Integer 
        deriving (Eq,Show)

    shiftLetter::Char -> Shift -> Char
    shiftLetter c (LeftShift x)
        | x==0 = c 
        | otherwise = shiftLetter c' (LeftShift (mod (x-1) 26))
            where c'
                    | pred c < 'a' = 'z'
                    | otherwise = pred c
    shiftLetter c (RightShift x)
        | x==0 = c 
        | otherwise = shiftLetter c' (RightShift (mod (x-1) 26))
            where c'
                    | succ c > 'z' = 'a'
                    | otherwise = succ c

    shift::String-> Shift -> String
    shift s s' = map (flip shiftLetter s') s
This entry was posted in Haskell. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.