Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 在方案中使用本地_List_Scheme_Local_Racket - Fatal编程技术网

List 在方案中使用本地

List 在方案中使用本地,list,scheme,local,racket,List,Scheme,Local,Racket,我只是随便玩玩,做了一堆函数,看看我是否可以在哪里使用local。这只是将点x和y相乘,并将结果加1。有没有办法替换我在这里创建的helper函数并使用local?当然,这似乎是使用local的好地方,只要helper过程只在购物车中使用: (define-struct point (x y)) (define (helper lon) (* (point-x lon) (point-y lon))) (define (cart lon) (cond [(emp

我只是随便玩玩,做了一堆函数,看看我是否可以在哪里使用local。这只是将点x和y相乘,并将结果加1。有没有办法替换我在这里创建的helper函数并使用local?

当然,这似乎是使用
local
的好地方,只要
helper
过程只在
购物车中使用:

(define-struct point (x y))


(define (helper lon)
  (* (point-x lon)
     (point-y lon)))

(define (cart lon)

  (cond
    [(empty? lon) 0]
    [else
     (+ (helper (first lon))
        (cart (rest lon))1)]))
还请注意,根据使用的语言,
local
可能不是必需的,简单的内部定义也可以做到这一点:

(define (cart lon)
  (local [(define (helper lon)
            (* (point-x lon)
               (point-y lon)))]
    (cond
      [(empty? lon) 0]
      [else
       (+ (helper (first lon))
          (cart (rest lon))
          1)])))

这就是问题所在。我总是把父母搞得一团糟。谢谢@Josh一个好的IDE会帮你做到这一点,过一会儿你就会忘记parens;)顺便说一句,我在回答中添加了一条附加信息,请参阅更新。
(define (cart lon)
  (define (helper lon)
    (* (point-x lon)
       (point-y lon)))
  (cond
    [(empty? lon) 0]
    [else
     (+ (helper (first lon))
        (cart (rest lon))
        1)]))