我创造了一些将单词转换成它们的对应物的明胶翻译器。
按照以下步骤,
(translate '("ark" "gold"))
给出
"arkway golday"
但是我想简单地输入(翻译" ark gold")并获得" arkway golday"
我想我会使用string-split和string-join但是我似乎在添加错误:
(define (piglatin phrase)
(string-split translate phrase))
并且跑步(piglatin'(" ark gold"))
给我错误:
string-split: contract violation
expected: (or/c string? regexp?)
given: '("ark gold")
代码:
#lang racket
(define (piglatin phrase)
(string-split translate phrase))
(define (translate sentence)
(string-join (map breakSentence sentence)))
; break down sentance string split into a list
(define (breakSentence word)
(list->string (listWord (string->list word))))
; break down word for vowel testing
(define (listWord word)
(cond
((foundVowel (car word))
(startsVowel word))
(else(noVowel word '()))))
; letters that are vowels, their prensence indicates way should be added to end
(define (foundVowel letter)
(member letter '(#\a #\e #\i #\o #\u #\y)))
; allow adding way to end of word
(define (startsVowel word)
(append word '(#\w #\a #\y)))
答案 0 :(得分:0)
在翻译之前,您需要将字符串拆分为单词。
(define (piglatin phrase)
(translate (string-split phrase)))