【问题标题】:flipping strings upside down racket [duplicate]颠倒球拍[重复]
【发布时间】:2013-02-18 09:44:35
【问题描述】:

我把字母倒过来了。 p to d 我似乎做不到的是让一串单词颠倒过来。就像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")

【问题讨论】:

  • 我想出了另一种(更简单)的方法来做到这一点,使用 beginng 学生语言。不要以为我曾经使用过 lambda

标签: scheme racket


【解决方案1】:

helper 程序上的几个 cmets:

  • 您应该为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"))
=> '("ɔ" "ɐ" "ʇ")

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

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

【讨论】:

  • 谢谢。但是拉姆达?您使用什么语言?我正在使用带有缩写的初学者。
  • @JerryLynch 查看我的更新答案,以获得没有maplambda 的解决方案
  • 我不太确定。它不应该类似于我的辅助功能吗?
  • 呜呜呜。我想到了!谢谢。
  • @JerryLynch 非常棒!我真的很高兴:)
猜你喜欢
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 2018-07-30
  • 2021-06-10
相关资源
最近更新 更多