2012-07-09 48 views
1

此處新方案。我試圖編譯一個方案功能,range。這很簡單 - 它需要一個start,stepstop列表L並創建一個新的列表,其中每個元素= stepAmt + curStep。無法編譯方案功能

例如:(範圍 '(0 2 7))=>(0 2 4 6),(範圍'(2 2 0))=>()

當我試圖編譯

(define (helper2(start stepAmt stop curStep newList) 
(if (> start stop) 
    '() 
    (if (> (+ stepAmt curStep) stop) 
     newList 
     (helper2 (start stepAmt stop (+ stepAmt curStep) (concat newList (+stepAmt curStep)))))))) 

我得到的錯誤

病態的特殊形式:(定義helper2(開始stepamt停止curstep newlist)(如果......))

我不確定這是什麼意思。我已經仔細檢查了我的邏輯和括號,並且無法弄清楚。

這是將調用該函數的功能:

(define (example L) 
(let (
    (start (car L)) 
    (curStep (car (cdr L))) 
    (step (car (cdr L))) 
    (stop (car (cdr (cdr L)))) 
    ) 
    (helper2 (start step stop curStep '())) 
) 

任何指針將是巨大的。我不確定這是錯誤還是邏輯錯誤。謝謝!

+0

嗨@iaacp。在示例函數中使用(helper2 start step stop curStep'()並在helper2中修復類似的問題。在Scheme中不允許插入額外的括號。(start step ...)中的括號表示:調用由請注意,Rajesh給出的解決方案沒有這樣的括號。 – soegaard 2012-07-09 07:32:19

+0

如果你有第二個問題,請把它作爲一個單獨的問題發佈。 – Marcin 2012-07-09 20:10:59

回答

4

你不必

(define helper2 (some arguments go here) 
    definition goes here) 

(define (helper2 some arguments go here) 
    definition goes here) 

要記住這一點,define之後的內容看起來就像調用你正在定義的函數一樣。 「以下是如何處理類似(helper2 some arguments go here)的呼叫:...」

+0

謝謝。我已經修復了錯誤,現在它編譯了,但是運行的並不正確:) – iaacp 2012-07-09 01:20:26

+2

你現在在'helper2'的主體中有一個類似的錯誤,你說'(helper2(start stepAmt ...) )'。我認爲你的大腦還沒有完全符合計劃中括號的內容。你只需要'(helper2 start stepAmt ...)'。在'example'的定義中有一個類似的問題(實際上可能是失敗的問題)。 – 2012-07-09 01:34:15

+0

你說得對。我一直在做這樣的愚蠢的錯誤。幸運的是,我現在已經能夠正確調試它 - 因爲我認爲所有括號問題都消失了。謝謝! – iaacp 2012-07-09 01:38:10

3

仔細看,你已經把括號:

(define helper2(start stepAmt stop curStep newList) ... 

(define (example L) ... 
+0

Agh!謝謝你。不幸的是,我還在你會看到其他的東西嗎? – iaacp 2012-07-09 01:08:01

+0

你會得到*完全相同的錯誤嗎?那個說「形式不規則的特殊形式:(define helper2(startstamt stop curstep newlist)(if ...。 ...))「?你是否記得在更改它之後保存你的源代碼? – 2012-07-09 01:08:50

+0

噢,我看到了,你改了代碼,但它仍然不正確,你現在有太多的括號 – 2012-07-09 01:15:48

2

您是否在使用DrRacket? 此作品:

#lang racket 

(define (helper2 start stepAmt stop curStep newList) 
(if (> start stop) 
    '() 
    (if (> (+ stepAmt curStep) stop) 
     newList 
     (helper2 start stepAmt stop (+ stepAmt curStep) (concat newList (+ stepAmt curStep)))))) 

(define (concat l elm) 
    (append l (list elm))) 

(define (example L) 
(let (
    (start (car L)) 
    (curStep (car (cdr L))) 
    (step (car (cdr L))) 
    (stop (car (cdr (cdr L)))) 
    ) 
    (helper2 start step stop curStep '()) 
))