Sha256: b7977fd69c6e488d9e0bd1ca21b59731c87e05215ce3549914bc79c88fc88e60

Contents?: true

Size: 1.13 KB

Versions: 84

Compression:

Stored size: 1.13 KB

Contents

module SecretHandshake (handshake) where

import Numeric (readInt)
import Data.Bits (testBit)

data Action = Wink
            | DoubleBlink
            | CloseYourEyes
            | Jump
            | Reverse
            deriving (Show, Eq, Enum, Bounded)

handshake :: Handshake a => a -> [String]
handshake = foldr f [] . toActions
  where f Reverse acc = reverse acc
        f action acc = toString action:acc
        toString action = case action of
          Wink          -> "wink"
          DoubleBlink   -> "double blink"
          CloseYourEyes -> "close your eyes"
          Jump          -> "jump"
          Reverse       -> undefined

class Handshake a where
  toActions :: a -> [Action]

instance Handshake Int where
  toActions n = [act | act <- Reverse:[Wink .. Jump], n `testBit` fromEnum act]

class BinParsable a where
  listToInt :: [a] -> Int

instance BinParsable Char where
    listToInt s = case readInt 2 isBin parseBin s of
      [(n, "")] -> n
      _         -> 0
      where isBin c = c == '0' || c == '1'
            parseBin = fromEnum . ('1'==)


instance BinParsable a => Handshake [a] where
  toActions = toActions . listToInt

Version data entries

84 entries across 84 versions & 1 rubygems

Version Path
trackler-1.0.1.1 tracks/haskell/exercises/secret-handshake/src/Example.hs
trackler-1.0.1.0 tracks/haskell/exercises/secret-handshake/src/Example.hs
trackler-1.0.0.1 tracks/haskell/exercises/secret-handshake/src/Example.hs
trackler-1.0.0 tracks/haskell/exercises/secret-handshake/src/Example.hs