我一直收到错误:“定义enigma_encode所需的Num Char实例”,我真的无法理解为什么。
据我所知,此错误消息与函数enigma_encode中的警卫相关联:
| or < 25
但我不明白为什么我在比较Int(在SimpleEnigma数据类型中定义的整数常量?
有人可以首先阐明他们的问题是什么,为什么会发生,也许还有一些方法可以解决它。
非常感谢瓦西里(谢菲尔德大学的计算机科学专业学生)
抱歉,我认为这是与enigna_encode中的SimpleEnigma'或'部分数据结构的比较错误。但是这里的整个代码是完整的:module Enigma where
import Cipher
import AssignmentHelp
--Rotor data structure definition
data Rotor = R1 | R2 | R3 | R4 | R5
--assigning rotor constants to cipher constants type definition
rtr_2_ciph :: Rotor -> Cipher
--assigning rotor constants to cipher constants function definition
rtr_2_ciph R1 = rotor1
rtr_2_ciph R2 = rotor2
rtr_2_ciph R3 = rotor3
rtr_2_ciph R4 = rotor4
rtr_2_ciph R5 = rotor5
--SimpleEnigma data structure definition
type SimpleEnigma = (Rotor, Rotor, Rotor, Int, Int, Int)
--Reflector data structure definition
type Reflector = [(Char,Char)]
--Standard reflector type defintion
st_reflector :: Reflector
st_reflector = [('A','Y'),('B','R'),('C','U'),('D','H'),('E','Q'),('F','S'),('G','L'),('I','P'),('J','X'),('K','N'),('M','O'),('T','Z'),('V','W')]
--Enigma Encode type definition
enigma_encode :: Char -> SimpleEnigma -> Char
--Enigma Encode function definition
enigma_encode x (lr, mr, rr, ol, om, or)
| or < 25 = reverse_encode (reverse_encode (reverse_encode (reflection (encode (encode (encode x (rtr_2_ciph rr) (or+1)) (rtr_2_ciph mr) om) (rtr_2_ciph lr) ol) st_reflector) (offset_cipher (rtr_2_ciph lr) ol)) (offset_cipher (rtr_2_ciph mr) om)) (offset_cipher (rtr_2_ciph rr) (or+1))
| or == 25 = reverse_encode (reverse_encode (reverse_encode (reflection (encode (encode (encode x (rtr_2_ciph rr) 0) (rtr_2_ciph mr) (om+1)) (rtr_2_ciph lr) ol) st_reflector) (offset_cipher (rtr_2_ciph lr) ol)) (offset_cipher (rtr_2_ciph mr) (om+1))) (offset_cipher (rtr_2_ciph rr) 0)
| or == 25 && om == 25 = reverse_encode (reverse_encode (reverse_encode (reflection (encode (encode (encode x (rtr_2_ciph rr) 0) (rtr_2_ciph mr) 0) (rtr_2_ciph lr) (ol+1)) st_reflector) (offset_cipher (rtr_2_ciph lr) (ol+1))) (offset_cipher (rtr_2_ciph mr) 0)) (offset_cipher (rtr_2_ciph rr) 0)
| or == 25 && om == 25 && ol == 25 = reverse_encode (reverse_encode (reverse_encode (reflection (encode (encode (encode x (rtr_2_ciph rr) 0) (rtr_2_ciph mr) 0) (rtr_2_ciph lr) 0) st_reflector) (offset_cipher (rtr_2_ciph lr) 0)) (offset_cipher (rtr_2_ciph mr) 0)) (offset_cipher (rtr_2_ciph rr) 0)
--Reflection type definition
reflection :: Char -> Reflector -> Char
--Reflection function definition
reflection x (h:t)
| x == fst h = snd h
| x == snd h = fst h
| otherwise = reflection x t
然后还有编码和反向编码和偏移函数以及任何相关的数据结构:
--1) Cipher data structure definition
type Cipher = String
--kept it as a string as easy to manipulate and work with using the in-built
--list functions due to string = [Char]
--Plan Text Cipher constant Definition
plain_cipher :: Cipher
plain_cipher = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
--just the standard alphabet used later, can be changed any string (ie another alphabet)
--encode type defintion
encode :: Char -> Cipher -> Int -> Char
--encode function definiton
encode x cipher offset
| validate_cipher cipher = offset_cipher cipher offset!!head (elemIndices x plain_cipher)
{--before encoding, validates the substitute cipher. Then if valid, offsets the cipher
and returns the encoded char in correlation to its index in the plain cipher.
the head of elemIndices is used to avoid having to deal with maybe int in elemIndex function--}
--offset the cipher type definition
offset_cipher :: Cipher -> Int -> Cipher
--offset the cipher function definition
offset_cipher cipher offset = drop (26 - mod offset 26) cipher ++ take (26 - mod offset 26) cipher
--takes any number offset, including those above 26 by dropping letters from the tail of the substitute cipher
--by 26 - (the mod of the offset by 26). Then adds the rest of the subsitute cipher to the end with take.
--reverseEncode type defintion
reverse_encode :: Char -> Cipher -> Char
--decode function definition
reverse_encode x cipher
| validate_cipher cipher = plain_cipher!!head (elemIndices x cipher)
{-- before reverse encoding, validates the cipher. If the cipher is valid
then again using elemindices (again to avoid maybe int) finds the correlating
index in the substitute cipher and uses that index in the plain cipher to decode the char --}
我只想提一下代码的第二部分中的任何内容:从cipher.hs(编码,偏移,反向编码)导入的内容都已经过测试和测试,并且完美地工作,因为它们应该是这就是为什么我不认为他们是问题的一部分,但我猜他们可能是某种方式:)
感谢您的评论如此之快,没有意识到社区的奉献精神!
在解释方面需要的任何其他东西或缺少的东西,只需评论谢谢:)
瓦西