For Programmers: Free Programming Magazines  


Home > Archive > Scheme > March 2007 > Counting Values in a List









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Counting Values in a List
mkinder@gmail.com

2007-03-22, 4:15 am

Hi,

I'm just learning scheme as I've heard it's a good language to get a
grasp of programming. I've written a simple program, and I've got it
returning 4 values where it should return 2.

Basically what it does is compare an "answer list" to a list of
guesses by the user. So if the user gets all 4 values correct, it
returns 1, 1, 1, 1, and if the user gets 2 right, 2 wrong, it returns
1, 1, 0, 0 (or wherever the user guessed incorrectly).

What I want it to do is return eg; (4, 0) instead.

Anyone got any ideas?

Phil Jackson

2007-03-22, 8:06 am

mkinder@gmail.com writes:

> What I want it to do is return eg; (4, 0) instead.
>
> Anyone got any ideas?


Can you post your code?

Cheers,
Phil
mkinder@gmail.com

2007-03-22, 8:06 am

On Mar 22, 8:23 pm, Phil Jackson <s...@place-spam-here.blah> wrote:
> mkin...@gmail.com writes:
>
>
> Can you post your code?
>
> Cheers,
> Phil


Well, this is the code that spits out the (1, 1, 1, 1) etc.

(define (user-turn guess-list answer-list

(user-turn

(cond
([equal? (first answer-list) (first guess-list)] 1)
([equal? (first answer-list) (second guess-list)] 0)
([equal? (first answer-list) (third guess-list)] 0)
([equal? (first answer-list) (fourth guess-list)] 0)
)

(cond
([equal? (second answer-list) (first guess-list)] 0)
([equal? (second answer-list) (second guess-list)] 1)
([equal? (second answer-list) (third guess-list)] 0)
([equal? (second answer-list) (fourth guess-list)] 0)
)

(cond
([equal? (third answer-list) (first guess-list)] 0)
([equal? (third answer-list) (second guess-list)] 0)
([equal? (third answer-list) (third guess-list)] 1)
([equal? (third answer-list) (fourth guess-list)] 0)
)

(cond
([equal? (fourth answer-list) (first guess-list)] 0)
([equal? (fourth answer-list) (second guess-list)] 0)
([equal? (fourth answer-list) (third guess-list)] 0)
([equal? (fourth answer-list) (fourth guess-list)] 1)
)
)


mkinder@gmail.com

2007-03-22, 8:06 am

On Mar 22, 8:40 pm, mkin...@gmail.com wrote:
> On Mar 22, 8:23 pm, Phil Jackson <s...@place-spam-here.blah> wrote:
>
> CODE


Whoops, missing a bracket in there


Nathan Thern

2007-03-22, 7:06 pm

mkinder@gmail.com wrote:
> Hi,
>
> I'm just learning scheme as I've heard it's a good language to get a
> grasp of programming. I've written a simple program, and I've got it
> returning 4 values where it should return 2.
>
> Basically what it does is compare an "answer list" to a list of
> guesses by the user. So if the user gets all 4 values correct, it
> returns 1, 1, 1, 1, and if the user gets 2 right, 2 wrong, it returns
> 1, 1, 0, 0 (or wherever the user guessed incorrectly).
>
> What I want it to do is return eg; (4, 0) instead.
>
> Anyone got any ideas?
>


If I understand correctly, your program returns a list and you want a
different list; e.g. a mapping like this:
(0 0 0 0) => (0 4)
(0 1 0 0) => (1 3)
(0 1 1 0) => (2 2)
(1 0 1 1) => (3 1)

Do you understand apply? apply + gives you the first element of the
result you want.

(let ((lst '(0 1 1 1)))
(apply + lst))
=> 3

Do you understand map? map lets you transform your original list. You
might want a transformation like:
(0 1 1 1) => (1 0 0 0)

(map (lambda (n) (- 1 n)) '(0 1 1 1))
=> (1 0 0 0)

Now you can get the second element of your desired result:
(let ((lst '(0 1 1 1))
(flip (lambda (n) (- 1 n))))
(apply + (map flip lst)))
=> 1

thus,
(define (num_right&wrong lst)
(let ((flip (lambda (n) (- 1 n))))
(list
(apply + lst)
(apply + (map flip lst))))

(num_right&wrong '(0 1 1 1))
=> (3 1)

demonstrates what you want.

There are many other ways to do it. The "schemish" way is to iterate
through a list using a tail recursive function:

(define (num_right&wrong lst)
(letrec
((sum_them
(lambda (lst right wrong)
(cond
((null? lst)
(list right wrong))
((zero? (car lst))
(sum_them (cdr lst) right (+ wrong 1)))
(else
(sum_them (cdr lst) (+ right 1) wrong))))))
(sum_them lst 0 0)))

(num_right&wrong '(0 1 1 1))
=> (3 1)

regards,
Nate T.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com