首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在列表中查找字符串

在列表中查找字符串
EN

Stack Overflow用户
提问于 2014-01-29 13:19:38
回答 2查看 113关注 0票数 0

我正在尝试修改我在SOF上找到的元音代码,发布在下面,我试图使它成为一个单词列表,以便我可以检查该单词是否在列表中。但我一直收到错误:

代码语言:javascript
复制
;; A list of words
(define words-list (cons #\dog (cons #\pig )
;; split-string : string -> (listof string-pieces)
;; converts a string into a list of string pieces.
(define (split-string a-string)
  (string->list a-string))
;; has-word? : string-piece -> boolealn
;; checks whether a string-piece is a vowel
(define (has-word? string-piece words)
  (cond ((empty? words) false)
    ((equal? string-piece (first words)) true)
    (else (has-word? string-piece (rest words)))))
;; Test
(check-expect (has-word? #\i word-list) true)
(check-expect (has-word? #\x word-list) false)
;; contains-words-list : (listof string-pieces) -> boolean
;; determines whether any items on a list of string-pieces
;; contains a piece that represents a word, from a list of words.
(define (contains-words-list losp)
  (cond ((empty? losp) false)
        ((false? (has-word? (first losp) words-list))
         (contains-words-list (rest losp)))
        (else (has-word? (first losp) words-list))))
;; Test
(check-expect (contains-word-list (cons #\h (cons #\i empty))) true)
(check-expect (contains-word-list (cons #\h (cons #\h empty))) false)
;; contains-word? : string -> boolean
;; checks whether a string contains a vowel.
(define (contains-word? a-string)
  (contains-word-list (split-string a-string)))
;; Test
(check-expect (contains-word? "pig") true)

我一直收到错误,比如狗和猪的cons太大了,它不会产生正确的输出,任何指导都是很好的。

EN

回答 2

Stack Overflow用户

发布于 2014-01-30 02:09:44

代码语言:javascript
复制
    ;; A list of words
    (define words-list (cons "dog" (cons "pig" '())))
    ;; split-string : string -> (listof string-pieces)
    ;; converts a string into a list of string pieces.
    (define (split-string a-string)
      (string->list a-string))
    ;; has-word? : string-piece -> boolealn
    ;; checks whether a string-piece is a vowel
    (define (has-word? string-piece words)
      (cond ((null? words) #f)
        ((equal? string-piece (car words)) #t)
        (else (has-word? string-piece (cdr words)))))

    ;; contains-words-list : (listof string-pieces) -> boolean
    ;; determines whether any items on a list of string-pieces
    ;; contains a piece that represents a word, from a list of words.
    (define (contains-words-list losp)
      (cond ((null? losp) #f)
            ((false? (has-word? (car losp) words-list))
             (contains-words-list (cdr losp)))
            (else (has-word? (car losp) words-list))))

    ;; contains-word? : string -> boolean
    ;; checks whether a string contains a vowel.
    (define (contains-word? a-string)
      (contains-word-list (split-string a-string)))
票数 0
EN

Stack Overflow用户

发布于 2014-01-30 02:42:17

用于创建字符串列表的语法是错误的:

代码语言:javascript
复制
> (cons #\dog (cons #\pig ))
Unhandled exception
 Condition components:
   1. &lexical
   2. &message: "invalid syntax"
   3. &irritants: (#\d #\o)
   4. &source-position:
       file-name: *stdin*
       character: 10

而且您缺少花括号。试一试

代码语言:javascript
复制
(list "dog" "pig")

为您的word-list。出于类似的原因,您的check-expect表单也是错误的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21422735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档