[plt-scheme] Processing lists
Improve your examples. Write them as proper function calls. Rewrite
them as tests then:
(check-expect (f 1 2 3) 4)
On Mar 5, 2009, at 9:30 PM, aditya shukla wrote:
> Thanks for the help guys , I did it like this.
>
> ;question in a given list find out how many times a symbol occurs
> in the list.
> ;contract : fun-with-lists s-list * symbol -> number
> ;purpose : this function takes an s-list and a symbol as input and
> finds out how many times the symbol occurs in the list.
> ;examples 'a -> () =0 , ('a) =1 , ('b) = 0 , ('b 'c 'a 'a) = 2 ,
> ('b 'c 'd) =0
> ;template (define fun-with-lists (lambda s-list sym) (cond [(empty?
> s-list)] [ else (first s-list) fun-with-lists(rest s-list])))
> (define fun-with-lists (lambda (s-list sym )
> (cond
> [(empty? s-list) 0]
> [else (+( more-fun-with-lists(first s-
> list) sym) ( fun-with-lists(rest s-list) sym))])))
>
> ;contract more-fun with lists (symbol * symbol -> number)
> ;purpose : this function returns 1 if the (first s-list) = sym
> otherwise 0
> (define more-fun-with-lists (lambda (sym1 sym2)
> (cond
> [(symbol=? sym1 sym2) 1]
> [else 0])))
>
> Earlier i was thinking of having a counter which i would increment
> whenever an occurrence is found .Any suggestions on how to think
> in more of a functional paradigm.
>
> Aditya
>
>
>
>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme