练习1.3 SICP错误:无法评估Racket中的表达式

时间:2018-05-12 03:32:07

标签: racket sicp mit-scheme

我正在从SICP做练习1.3。

我的代码如下:

#lang racket

(require sicp)

(define (square a)
  (* a a)
    )

(define (sum-of-squares a b)
 (+ (square a) (square b)   )
                              )

(define (max a b)
(cond ((>= a b) a)
      (else b)
               )
                   )

(define (sum-of-biggest-squares a b c    )

(cond ((>= a b)
  (sum-of-squares a (max b c) )

  (sum-of-squares b (max a c) ) 

                        )
                        )
                            )

(sum-of-biggest-squares 5 7 10)

令人惊讶的是,Racket解释器

不打印上述任何结果。口译员

适用于其他值。但是对于

这三个值的集合不是

的工作。

当我尝试添加else语句时

如下:

  (else (sum-of-squares b (max a c) ) ) 

口译员说:

   exercise_1-3.rkt:23:10: else: not allowed as an expression
   in: (else (sum-of-squares b (max a c)))

1 个答案:

答案 0 :(得分:0)

您在函数else中有几个语法错误:应添加一个括号以关闭第一个(define (sum-of-biggest-squares a b c) (cond ((>= a b) (sum-of-squares a (max b c))) (else (sum-of-squares b (max a c))))) 子句,并将public class AES256Hash { private static byte[] key; private static Object mutex = new Object(); private const int KEY_SIZE = 256; private const int BLOCK_SIZE = 128; private const int IV_KEY_SIZE = 16; // 16 byte private AES256Hash() { } /// <summary> /// Create a new randomized cipher on each startup /// </summary> private static void InitializeCipherKey() { lock (mutex) { if (key == null) { key = new byte[32]; var cr = new System.Security.Cryptography.RNGCryptoServiceProvider(); cr.GetBytes(key, 0, key.Length); } } } /// <summary> /// Converts the input data into an IV key /// </summary> /// <param name="Data"></param> /// <returns></returns> private static byte[] CreateIvKey(string Data) { byte[] IvKey = new UTF8Encoding().GetBytes(Data); if (IvKey.Length != IV_KEY_SIZE) { byte[] NewTruncatedIvKey = new byte[IV_KEY_SIZE]; Buffer.BlockCopy(IvKey, 0, NewTruncatedIvKey, 0, Math.Min(IV_KEY_SIZE, IvKey.Length)); // the rest of the bytes are 0 padded return NewTruncatedIvKey; } return IvKey; } /// <summary> /// Encrypts a string with AES256 with the given key and string data /// </summary> /// <param name="Key"></param> /// <param name="Data"></param> /// <returns></returns> public static string EncryptString(string Key, string Data) { InitializeCipherKey(); byte[] IvKey = CreateIvKey(Key); byte[] dataB = new UTF8Encoding().GetBytes(Data); using (AesCryptoServiceProvider csp = new AesCryptoServiceProvider()) { csp.Padding = PaddingMode.PKCS7; csp.Mode = CipherMode.ECB; csp.KeySize = KEY_SIZE; csp.BlockSize = BLOCK_SIZE; csp.Key = key; csp.IV = IvKey; ICryptoTransform encrypter = csp.CreateEncryptor(); return Convert.ToBase64String(encrypter.TransformFinalBlock(dataB, 0, dataB.Length)); } } /// <summary> /// Decrypts a string with AES256 with the given key and string data /// </summary> /// <param name="Key"></param> /// <param name="Data"></param> /// <returns></returns> public static string DecryptString(string Key, string Data) { InitializeCipherKey(); byte[] IvKey = CreateIvKey(Key); byte[] dataB = Convert.FromBase64String(Data); using (AesCryptoServiceProvider csp = new AesCryptoServiceProvider()) { csp.Padding = PaddingMode.PKCS7; csp.Mode = CipherMode.ECB; csp.KeySize = KEY_SIZE; csp.BlockSize = BLOCK_SIZE; csp.Key = key; csp.IV = IvKey; ICryptoTransform decrypter = csp.CreateDecryptor(); return new UTF8Encoding().GetString( decrypter.TransformFinalBlock(dataB, 0, dataB.Length) ); } } } 添加到第二个:{{1}}:

{{1}}

请注意,格式化代码的方式与当前的常见约定不同,因此很难读取它并且很容易引入语法错误。