翻转字符串倒置球拍

时间:2013-02-17 23:37:29

标签: scheme racket

我收到了颠倒的信件。 pd我似乎无法做的是将一串字颠倒过来。与house完全相同,ǝsnoɥ。任何帮助,将不胜感激。我刚刚完成了。我正在使用列表缩写的初学者。

#;(first (first l2))
#;(first (rest l2))
#;(first (rest (first l2)))

(define (helper character list)
  (cond [(string=? character (first (first list))) (first (rest (first list)))]
        [else (helper character (rest list))]))

(check-expect (helper "p" table-1) "d")
(check-expect (helper "t" table-1) "ʇ")

;;_______________________________________________________________________________

(define (upside-down string)


(upside-down "cat")

1 个答案:

答案 0 :(得分:1)

关于helper程序的几条评论:

  • 如果character不在列表中,您应该为案例添加额外条件 - 如果发生这种情况,只需返回相同的character。否则,将发生错误
  • 参数不应该命名为list,这是一个内置程序,覆盖它是一种不好的做法
  • 在继续之前彻底测试。

一旦整理好了,以下是如何实施upside-down,填写空白:

(define (upside-down str)
  (<???>                       ; finally, do the opposite of explode on the list
   (map (lambda (c)            ; map over each of the characters
          (<???> <???> <???>)) ; call helper on the character
        (<???> (<???> str))))) ; reverse the exploded string

如果您不允许使用maplambda,请实施convert过程,该过程迭代字符串中的每个字符并应用helper每一个,返回一个包含应用结果的列表 - 基本上,实现你自己的map始终将helper应用于输入列表中的每个元素:

(define (convert lst)
  <???>)

(convert '("c" "a" "t"))
=> '("ɔ" "ɐ" "ʇ")

现在我们可以使用upside-down编写convert

(define (upside-down str)
  (<???>                   ; finally, do the opposite of explode on the list
   (convert                ; use our shiny new procedure
    (<???> (<???> str))))) ; reverse the exploded string
相关问题