beginners "let"/"random" question 
Author Message
 beginners "let"/"random" question

Hi!

This being my first post to this list, I apologize if this is inappropiate.

This is a newbie question. My problem is as follows: I am trying to create a
function that takes a list of atoms as input, and returns two strings, which
are parts of the original string split in two at a random location. Example
would be

Quote:
> (split '(0 1 1 0))
((0) (1 1 0))
> (split '(0 1 1 0))

((0 1) (1 0))

I tried using something like
(define (split lst)
    (let ((split-point (random (length lst))))
        (cons (first-part split-point lst)
                 ( last-part split-point lst))))))

Where (first-part) anda (last-part) are functions that takes a number and a
list as arguments, and returns the elements of the list before and after the
number.

The problem seems to be that the (rand) function is evaluated every time I
refer to (split-point) in the function, so the split-point does not stay
constant through the function.

Any help would be appreciated, as I'm a stuck here.

Martin



Wed, 26 Nov 2003 15:02:59 GMT  
 beginners "let"/"random" question

Quote:

>(define (split lst)
>    (let ((split-point (random (length lst))))
>        (cons (first-part split-point lst)
>                 ( last-part split-point lst))))))

>The problem seems to be that the (rand) function is evaluated every time I
>refer to (split-point) in the function, so the split-point does not stay
>constant through the function.

That's not your problem.  

To convince yourself:

(define (test n)
  (let ((r (random n)))
    (list r r r r r)))

Perhaps your first-part and last-part functions don't work the
way you expect...try some things like

(first-part 2 '(a b c d e f g))
(last-part  2 '(a b c d e f g))

and see what you get.

--

http://www.people.cornell.edu/pages/jmv16/



Thu, 27 Nov 2003 03:39:12 GMT  
 beginners "let"/"random" question


Quote:


>>(define (split lst)
>>    (let ((split-point (random (length lst))))
>>        (cons (first-part split-point lst)
>>                 ( last-part split-point lst))))))

>>The problem seems to be that the (rand) function is evaluated every time I
>>refer to (split-point) in the function, so the split-point does not stay
>>constant through the function.

>That's not your problem.  

>To convince yourself:

>(define (test n)
>  (let ((r (random n)))
>    (list r r r r r)))

>Perhaps your first-part and last-part functions don't work the
>way you expect...try some things like

>(first-part 2 '(a b c d e f g))
>(last-part  2 '(a b c d e f g))

>and see what you get.

A better test, in case the functions are destructive, might be:

(let ((lst (list 1 2 3 4 5 6)))
  (write (first-part 2 lst))
  (write (last-part 2 lst)))

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Fri, 28 Nov 2003 22:51:05 GMT  
 beginners "let"/"random" question
You are both right - my (first-part) and (last-part) functions didn't work
as I intended them to.

I fixed this now, and I'm moving on (to my next problem ;-).

Thanks a lot!

I appreciate it.

Martin


Quote:




> >>(define (split lst)
> >>    (let ((split-point (random (length lst))))
> >>        (cons (first-part split-point lst)
> >>                 ( last-part split-point lst))))))

> >>The problem seems to be that the (rand) function is evaluated every time
I
> >>refer to (split-point) in the function, so the split-point does not stay
> >>constant through the function.

> >That's not your problem.

> >To convince yourself:

> >(define (test n)
> >  (let ((r (random n)))
> >    (list r r r r r)))

> >Perhaps your first-part and last-part functions don't work the
> >way you expect...try some things like

> >(first-part 2 '(a b c d e f g))
> >(last-part  2 '(a b c d e f g))

> >and see what you get.

> A better test, in case the functions are destructive, might be:

> (let ((lst (list 1 2 3 4 5 6)))
>   (write (first-part 2 lst))
>   (write (last-part 2 lst)))

> --

> Genuity, Burlington, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the

group.


Sun, 30 Nov 2003 03:56:34 GMT  
 beginners "let"/"random" question
Could you please post the incorrect output you get?

It might be ((0 1) 1 0)

If last-part does what it seems to do, 'cons' may not be the function you want
to use. Try 'list'.

--
Biep
Reply via http://www.biep.org


Quote:
> Hi!

> This being my first post to this list, I apologize if this is inappropiate.

> This is a newbie question. My problem is as follows: I am trying to create a
> function that takes a list of atoms as input, and returns two strings, which
> are parts of the original string split in two at a random location. Example
> would be

> > (split '(0 1 1 0))
> ((0) (1 1 0))
> > (split '(0 1 1 0))
> ((0 1) (1 0))

> I tried using something like
> (define (split lst)
>     (let ((split-point (random (length lst))))
>         (cons (first-part split-point lst)
>                  ( last-part split-point lst))))))

> Where (first-part) anda (last-part) are functions that takes a number and a
> list as arguments, and returns the elements of the list before and after the
> number.

> The problem seems to be that the (rand) function is evaluated every time I
> refer to (split-point) in the function, so the split-point does not stay
> constant through the function.

> Any help would be appreciated, as I'm a stuck here.

> Martin



Fri, 05 Dec 2003 23:11:01 GMT  
 beginners "let"/"random" question


Quote:
>Could you please post the incorrect output you get?

He posted almost a week ago that he fixed it.  He had bugs in the
first-part and last-part procedures.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sat, 06 Dec 2003 01:36:14 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

2. BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}

3. Parsing ""D""?

4. "Fifth", "Forth", zai nar?

5. Ruby "finalize", "__del__"

6. ANNOUNCE: new "plus"- and "dash"-patches available for Tcl7.5a2/Tk4.1a2

7. Looking for "stdin", "stdout"

8. Yet Another "Thickie" question / Random Range

9. Beginner questions re: "philosophy"

10. Beginner's question:"Hello World"

11. Beginner's question:"Hello World"

12. Beginner question on function "LENGTH"

 

 
Powered by phpBB® Forum Software