我想获得两个字符串作为参数,并检查第一个字符串是否是第二个字符串的开头。我无法理解,因为我不知道如何将字符串作为函数的参数。
(define starts-with( lambda (prefix str)
(define str2 (string->list (str)))
(define prefix2 (string->list (prefix)))
( cond ( (= (string-length(prefix2) 0) display "#t")
( (= car(prefix2) car(str2)) (starts-with (cdr(prefix2)cdr(str2) ) ) )
( display "#f")))))
Error: application: not a procedure; expected a procedure that can be
applied to arguments
给出:“ab” 参数......:[无]
任何人都可以解释一下我的错误是什么,一般来说,方案如何与列表或字符串一起使用..?我希望:
(starts-with "baz" "bazinga!") ;; "#t"
答案 0 :(得分:1)
问题不在于如何将字符串作为参数传递,问题是......您必须首先了解Scheme的工作原理。括号位于所有错误的位置,有些丢失,有些是不必要的,并且您调用过程的方式不正确。您的代码中存在很多错误,需要完全重写:
(define (starts-with prefix str)
(let loop ((prefix2 (string->list prefix)) ; convert strings to char lists
(str2 (string->list str))) ; but do it only once at start
(cond ((null? prefix2) #t) ; if the prefix is empty, we're done
((null? str2) #f) ; if the string is empty, then it's #f
((equal? (car prefix2) (car str2)) ; if the chars are equal
(loop (cdr prefix2) (cdr str2))) ; then keep iterating
(else #f)))) ; otherwise it's #f
请注意原始实现中的以下错误:
let
的好主意 - 它只是一个递归过程的语法糖,而不是真的一个循环display
要返回的值,只需返回它们=
来比较字符,正确的方法是使用char=?
或equal?
,这是更通用的cond
的最后一个条件应该是else
(f x)
和不,如下所示:f(x)
。此外,您不能将()
放在的某些内容上,除非您打算将其称为函数,这就是为什么:(str)
产生错误的原因application: not a procedure; expected a procedure that can be applied to arguments