0

I wrote a program below for "Define a procedure leet-speak takes a string and returns the result of changing all s's to fives, all e's to threes, all l's to ones, and all o's to zeros. Do not write any recursive code to do this. Simply make use of string->list, map, and list->string."

The error I got is:

 ~ (leet-speak "leet speak neat speak")
 Exception: attempt to apply non-procedure (1 3 3 #\t #\space 5 ...)

Here is my definition for leet-speak:

(define leet-speak
  (lambda (y)
    (list->string 
      ((map 
         (lambda (x)
           (cond
             [(eq? #\l x) 1]
             [(eq? #\s x) 5]
             [(eq? #\o x) 0]
             [(eq? #\e x) 3]
             [else x])
           ) (string->list y )))))

I really can't find out where the problem is.

4

1 回答 1

2

.周围的括号太多map。删除多余的,以便在 之前只有一个括号map,你应该很高兴。

cond还需要返回与数字对应的字符,而不是数字本身。另外,请考虑使用 acase而不是cond您拥有的。

总而言之,它的外观如下:

(define (leet-speak str)
  (list->string 
   (map (lambda (x)
          (case x
            [(#\l) #\1]
            [(#\s) #\5]
            [(#\o) #\0]
            [(#\e) #\3]
            [else x]))
        (string->list str))))
于 2013-04-24T06:16:18.807 回答