scheme - how to do a count in RACKET -
i'm trying write code in racket , know how solve i'm having trouble , can use .
the function list , specific symbol , , need return number of times symbol shown in list . in test - i'm comparing result number i'm asking , should return true if number same .
i've tried (if / cond / , tried acc ) - there missing .
here code including test . please me find out how write .
the idea of solution , take head of list , check if it's equal symbol wrote , if - n plus 1 , empty list equal 0 .
( : counts : (listof symbol) -> integer ) (define (counts n ) ; = list of symbols. (cond [(null? a) 0]) [(eq?(first a) 'x) (= n(+ n 1))] (counts( (rest a) n))) ;test: (test (eq? (counts ('a 'b 'x) )1))
there several problems code:
- the
cond
expression being incorrectly used, ,else
case missing - there erroneous parentheses, example @ end of second line in
counts
, when callcounts
in fourth line - in base case of recursion must return
n
, counter - you must call recursion if symbol found, in second case
- this part:
(= n (+ n 1))
not doing think, it's not changing value ofn
, instead testing equality betweenn
,(+ n 1)
(which return false, of course) - you're not passing parameter symbol being searched, it's hard-coded in procedure
this corrected version of intended write:
(define (counts x n) (cond [(null? a) n] [(eq? (first a) x) (counts (rest a) x (+ n 1))] [else (counts (rest a) x n)]))
use this:
(counts '(a b x c d x e x) 'x 0) => 3
i advice grab book or tutorial, seems you're struggling basic syntax. , learn how use drracket syntax errors, it's excellent ide.
Comments
Post a Comment