For Programmers: Free Programming Magazines  


Home > Archive > Scheme > December 2005 > list-ref-add1









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 list-ref-add1
Strake

2005-12-13, 7:06 pm

Hi all,

I'm writing a procedure that takes as arguments a list l and a number
k, and returns the list with the kth element incremented by 1. So far,
the code is:

(define list-head (lambda (l k)
(if (= k 0)
()
(cons (car l) (list-head (cdr l) (sub1 k))))))

(define list-ref-add1 (lambda (l k)
(append (list-head l (sub1 k)) (list (add1 (list-ref l k))) (cdr
(list-tail lk)))))

but then, when i do
(list-ref-add1 '(1 1 3) 2)

it gives me
(1 4)

I am quite sure the problem is in list-ref-add1 rather than list-head,
but i can't pinpoint it.

Bruce Lewis

2005-12-13, 7:06 pm

"Strake" <strake888@gmail.com> writes:

> I am quite sure the problem is in list-ref-add1 rather than list-head,
> but i can't pinpoint it.


Have you tried putting this code into DrScheme and using the stepper?
Strake

2005-12-13, 7:06 pm

It gives me strange errors:
function call: expected a defined name or a primitive operation name
after an
open parenthesis, but nothing's there

Nils M Holm

2005-12-13, 7:06 pm

Strake <strake888@gmail.com> wrote:
> but then, when i do
> (list-ref-add1 '(1 1 3) 2)
>
> it gives me
> (1 4)


What did you want it to give: (1 1 4) or (1 2 3)?

--
Nils M Holm <n m h @ t 3 x . o r g> -- http://www.t3x.org/nmh/
Bruce Lewis

2005-12-13, 7:06 pm

"Strake" <strake888@gmail.com> writes:

> It gives me strange errors:
> function call: expected a defined name or a primitive operation name
> after an
> open parenthesis, but nothing's there


Change () to '() in your code. The latter is the proper code to
evaluate to the empty list.
Jens Axel Søgaard

2005-12-13, 7:06 pm

Bruce Lewis wrote:
> "Strake" <strake888@gmail.com> writes:
>
>
>
>
> Change () to '() in your code. The latter is the proper code to
> evaluate to the empty list.


Actually he needs to use empty in the beginner language.

Strake, here is a version of your code that runs in the
beginner language. I have not fixed your mistakes, only
changed () to empty, and removed list-tail, which
isn't in the beginner language.

(define list-head (lambda (l k)
(if (= k 0)
empty
(cons (car l) (list-head (cdr l) (sub1 k))))))

(define list-ref-add1 (lambda (l k)
(append (list-head l (sub1 k))
(list (add1 (list-ref l k)))
(cdr l))))

--
Jens Axel Søgaard
Strake

2005-12-13, 7:06 pm

(1 2 3)

Strake

2005-12-13, 7:06 pm

list-head worked before, but i changed it and it still works. I'll keep
it as '()

It's list-ref-add1 that's giving me trouble

Strake

2005-12-13, 7:06 pm

I will be using mzscheme, it's just that someone suggested i use
drscheme for debugging purposes.

matteo d'addio 81

2005-12-13, 7:06 pm


Strake ha scritto:

> (1 2 3)


Try to test your function with more inputs and see what
happens.

matteo

Strake

2005-12-13, 7:06 pm

When i do
(list-ref-add1 '(1 2 3 4 4 6 7 8 9) 5)

it gives me
(1 2 3 4 7 7 8 9)

It should be
(1 2 3 4 5 6 7 8 9)

Jens Axel Søgaard

2005-12-13, 7:06 pm

Strake wrote:
> I will be using mzscheme, it's just that someone suggested i use
> drscheme for debugging purposes.


Bruce's excellent advise to use the stepper to see what's wrong
requires you to use DrScheme.

If you are using MzScheme, then the error message you saw,
is not due to an unquoted (). MzScheme accepts () with
no problems (but note, that other implementations
might require to quote the empty list).

--
Jens Axel Søgaard

Nils M Holm

2005-12-14, 4:01 am

Strake <strake888@gmail.com> wrote:
> I wrote:
> (1 2 3)


(Please do include some context when posting follow-ups.)

What does your answer imply?

Hint:

You want (list-ref-add1 '(1 1 3) 2) => (1 2 3)

but what does

(list-ref '(1 1 3) 2) give?

--
Nils M Holm <n m h @ t 3 x . o r g> -- http://www.t3x.org/nmh/
Strake

2005-12-14, 7:58 am

(list-ref-add1 '(1 1 3) 2) gives me (1 4)
It's somehow eating one of the elements

Jussi Piitulainen

2005-12-14, 7:58 am

Strake writes:

> When i do
> (list-ref-add1 '(1 2 3 4 4 6 7 8 9) 5)
>
> it gives me
> (1 2 3 4 7 7 8 9)


Try (list-ref-add1 '(1) 0) and see the light.
Sponsored Links







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

Copyright 2008 codecomments.com