Re: [racket-users] Struggling with writing array generator function

2017-07-13 Thread Vasily Rybakov
Well, actually that will be nested vectors, I just used list as example.

I thought about storing arrays as one-dimentional vectors, but it makes hard to 
work with literal arrays. If I use nested vectors, then I can write something 
like:

(matrix-* A #[#[1 2 3] #[4 5 6]])

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Struggling with writing array generator function

2017-07-13 Thread Vasily Rybakov
Daniel, thank you for your response!

It really helped. I've focuded too much on recursion and tried to put nested 
array generation into it, without realizing that I can use external loop.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Struggling with writing array generator function

2017-07-13 Thread Daniel Prager
Hi Vasily

Since you insist ... ;-)

The main issue is how to reduce the number of args with each step of the
recursion: looks like a job for curry!

Here's one way to do it:

(define (generate-array n f)
  (for/list ([i 3])
(if (= n 1)
(f i)
(generate-array (sub1 n) ((curry f) i)

(generate-array 1 (lambda (i) 10))
(generate-array 1 (lambda (i) i))
(generate-array 2 (lambda (i j) (+ (* 10 i) j)))

A more robust version would introspect the number of arguments to f and
drop the need for an explicit n.
Also should guard against the n = 0 case.

Dan

On Fri, Jul 14, 2017 at 6:07 AM, Vasily Rybakov 
wrote:

> On Thursday, July 13, 2017 at 9:30:44 PM UTC+2, Daniel Prager wrote:
> > It's straightforward to design a recursive macro with similar intent,
> but different surface syntax:
> >
> > (generate-array (i) 10)
> > (generate-array (i) i)
> > (generate-array (i j) (+ (* 10 i) j))
> >
> >
> > Dan
>
>
> Thanks for the answer, but I really need to pass generating function to it.
>
> I know that expended recursion shiuld look something like this (for
> examples of functions of 1, 2 and 3 arguments):
>
> ((lambda (f) (build-vector 3 (lambda (x) (f x (lambda (i) i))
> ((lambda (f) (build-vector 3 (lambda (x) (build-vector 3 (lambda (y) (f x
> y)) (lambda (i j) (+ i j)))
> ((lambda (f) (build-vector 3 (lambda (x) (build-vector 3 (lambda (y)
> (build-vector 3 (lambda (z) (f x y z (lambda (i j k) 10))
>
>
> But I don't know how to make such a recursion, that will produce this
> expansions.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Struggling with writing array generator function

2017-07-13 Thread Neil Van Dyke
(If this is for a school assignment, and the following comments are 
confusing, please disregard them.  The comments might make a school 
assignment harder than it's supposed to be.)


Are you sure you want your n-dimensional arrays to be implemented as 
nested Racket lists?


What about a 1-dimensional Racket vector representation?  Very simple 
arithmetic turns an n-dimensional index into a 1-dimensional index into 
the vector.


This is how lower-level languages often lay out fixed n-dimensional 
arrays in contiguous bytes/words of memory.


https://en.wikipedia.org/wiki/Row-_and_column-major_order

If you're doing high-performance numeric work, you can even lay out 
native number arrays in ways that let you use super-fast specialized CPU 
instructions on them.  But that might be counterproductive for your 
immediate Racket needs.  But the flat vector is still a valid thing to 
do in Racket.


Reasons you might not want to do a flat vector is if you have sparse 
arrays that are very large, or you have sharing between arrays or parts 
of the same array.


--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Struggling with writing array generator function

2017-07-13 Thread Vasily Rybakov
On Thursday, July 13, 2017 at 9:30:44 PM UTC+2, Daniel Prager wrote:
> It's straightforward to design a recursive macro with similar intent, but 
> different surface syntax:
> 
> (generate-array (i) 10)
> (generate-array (i) i)
> (generate-array (i j) (+ (* 10 i) j))
> 
> 
> Dan


Thanks for the answer, but I really need to pass generating function to it.

I know that expended recursion shiuld look something like this (for examples of 
functions of 1, 2 and 3 arguments):

((lambda (f) (build-vector 3 (lambda (x) (f x (lambda (i) i))
((lambda (f) (build-vector 3 (lambda (x) (build-vector 3 (lambda (y) (f x 
y)) (lambda (i j) (+ i j)))
((lambda (f) (build-vector 3 (lambda (x) (build-vector 3 (lambda (y) 
(build-vector 3 (lambda (z) (f x y z (lambda (i j k) 10))


But I don't know how to make such a recursion, that will produce this 
expansions.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Struggling with writing array generator function

2017-07-13 Thread Daniel Prager
It's straightforward to design a recursive macro with similar intent, but
different surface syntax:

(define-syntax generate-array
  (syntax-rules ()
[(_ (i) exp) (for/list ([i 3]) exp)]
[(_ (i j...) exp) (for/list ([i 3]) (generate-array (j...) exp))]))

(generate-array (i) 10)
(generate-array (i) i)
(generate-array (i j) (+ (* 10 i) j))

Dan

On Fri, Jul 14, 2017 at 4:52 AM, Vasily Rybakov 
wrote:

> I need to write function generete-array:
>
> (define (generate-array num-dimensions generator)
>...)
>
> such that it generate num-dimensions array (each dimension is of size 3)
> and uses generator function to produce elements of array (generator accepts
> num-dimensions arguments and returns number; each element is equal to the
> result of passing its indices to generator).
>
> So
>
> (generate-array 1 (lambda (i) 10)) => '(10 10 10)
>
> (generate-array 1 (lambda (i) i)) => '(0 1 2)
>
> (generate-array 2 (lambda (i j) (+ (* 10 i) j))) =>
>   => '((0 1 2) (10 11 12) (13 14 15))
>
> and so on...
>
> I think it should use recursion, because I don't know in advance the
> number of dimensions, but I can't figure out how to do it and where I
> should begin with.
>
> Any advice on which path to take for wrighting this function will be
> helpful.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
*Daniel Prager*
Agile/Lean Coaching, Innovation, and Leadership
Profile: skillfire.co/dan
Startups: youpatch.com , skillfire.co
Twitter: @agilejitsu 
Blog: agile-jitsu.blogspot.com
Linkedin: au.linkedin.com/in/danielaprager

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.