public inbox for
 help / color / mirror / Atom feed
From: Per Bothner <>
To: Peter Lane <>,
Subject: Re: Java arrays to Scheme
Date: Fri, 03 Feb 2017 23:24:00 -0000	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <>

On 02/03/2017 12:33 PM, Peter Lane wrote:
> Hi list,
> I'm trying to understand how to interact with Java functions which return arrays, e.g. double[][] or int[].
> When I call a function which returns an int[] or double[][] they display as numbers within [ ... ] square brackets.  I can extract their length and elements, e.g. using (x 0) etc where x is the array.
> What kind of object are these in the Scheme universe?  array? vector?

They are Java arrays.  They are not Scheme vectors or arrays.
However, they are "vector-like", and can be converted to other sequence types
using splices.

(The '!' operator is roughly the same as define-constant.  Using either
improves type inference in the REPL.)

#|kawa:1|# (! ar (int[] 4 -5 6))
#|kawa:2|# ar
[4 -5 6]

> I would like to convert these Java arrays into a Scheme list or vector, which should be a simple function to write.  But is there a function/trick like that already?

#|kawa:3|# (vector @ar)
#(4 -5 6)
#|kawa:4|# (list @ar)
(4 -5 6)
#|kawa:5|# (s32vector @ar)
#s32(4 -5 6)
#|kawa:6|# (u32vector @ar)
#u32(4 4294967291 6)

Note the splice operator makes a copy - it does not share the array.

However, you can use the ->sequence cast to make them share.
(The syntax (->TYPE VALUE) is generally the same as (as TYPE VALUE).)

#|kawa:7|# (->sequence ar)
#s32(4 -5 6)
#|kawa:8|# (! seq (->sequence ar))
#|kawa:9|# seq
#s32(4 -5 6)
#|kawa:10|# (set! (ar 2) 16)
#|kawa:11|# ar
[4 -5 16]
#|kawa:12|# seq
#s32(4 -5 16)

#|kawa:13|# (set! (seq 0) 8)
java.lang.ClassCastException: gnu.math.IntNum cannot be cast to java.lang.Integer
	at gnu.lists.S32Vector.setRaw(
	at gnu.lists.AbstractSequence.set(
	at gnu.kawa.functions.Setter$SetList.apply2(
	at gnu.mapping.Procedure.apply(
	at gnu.mapping.Procedure.apply(
	at gnu.mapping.CallContext.runUntilDone(
	at gnu.expr.ModuleExp.evalModule2(
	at gnu.expr.ModuleExp.evalModule(
	at kawa.repl.processArgs(
	at kawa.repl.main(

Ooops - that really should work.  But we can use a cast:

#|kawa:14|# (set! (seq 0) (->int 8))
#|kawa:15|# seq
#s32(8 -5 16)
#|kawa:16|# ar
[8 -5 16]

Casting to a u32vector is trickier:

#|kawa:17|# (->u32vector ar)
/dev/stdin:17:14: warning - type int[] is incompatible with required type u32vector
java.lang.ClassCastException: [I cannot be cast to gnu.lists.U32Vector
	at gnu.expr.ModuleExp.evalModule2(
	at gnu.expr.ModuleExp.evalModule(
	at kawa.repl.processArgs(
	at kawa.repl.main(

Oops - that really should work, too ...

You can use a raw constructor, though:

#|kawa:19|# (gnu.lists.U32Vector ar)
#u32(8 4294967291 16)
	--Per Bothner

      reply	other threads:[~2017-02-03 23:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-03 20:33 Peter Lane
2017-02-03 23:24 ` Per Bothner [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).