Accessor values question

Discussion of Common Lisp
Post Reply
macrolyte
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Accessor values question

Post by macrolyte » Thu May 22, 2014 7:28 am

Hey,
is there a function like values that would allow me to do this?

Code: Select all

       (* (values 3 4)) ;; doesn't work, but is there something that does?
Thanks.

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Accessor values question

Post by edgar-rft » Thu May 22, 2014 10:12 pm

Sorry, but it's not really clear what you mean. If you want to use #'* with multiple values then use MULTIPLE-VALUE-CALL:

Code: Select all

(multiple-value-call #'* (values 3 4))  => 12
If you want to replace VALUES by a different function then you could use APPLY and LIST:

Code: Select all

(apply #'* (list 3 4))  => 12
Is this what you're looking for?

macrolyte
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Re: Accessor values question

Post by macrolyte » Fri May 23, 2014 10:29 am

edgar-rft wrote:Sorry, but it's not really clear what you mean. If you want to use #'* with multiple values then use MULTIPLE-VALUE-CALL:

Code: Select all

(multiple-value-call #'* (values 3 4))  => 12
If you want to replace VALUES by a different function then you could use APPLY and LIST:

Code: Select all

(apply #'* (list 3 4))  => 12
Is this what you're looking for?
YES! Both methods are more than sufficient. One further question in regards to values, the objects returned by the function aren't really a list:

Code: Select all


(funcall (funcall (lambda(f)(lambda(a)(apply f a)))#'*) (values 3 7)) ;; I initially used FUNCALL in the lambda body

;; returns : APPLY: argument list given to * is dotted (terminated by 3) ???!!

(funcall (funcall (lambda(f)(lambda(a)(apply f a)))#'*) '(3 7)) 

;; returns : 21

(which is why I was having trouble from the beginning), so what exactly is happening with the structure(?) of the values returned by values? Thanks for the assistance. Pax.

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Accessor values question

Post by edgar-rft » Fri May 23, 2014 2:46 pm

Code: Select all

(funcall (funcall (lambda (f) (lambda (a) (apply f a))) #'*) (values 3 7))
=> APPLY: argument list given to * is dotted (terminated by 3)
The error message is misleading. IMO this is is a bug in your Common Lisp implementation. A correct error message should read "The value 3 is not of type LIST" or similar.

The reason is that Common Lisp functions do not handle multiple values by default:

Code: Select all

(funcall (lambda (a) ...) (values 3 7))
Here the parameter variable a is bound to the the primary value (the integer 3), and all following values (the integer 7) are ignored.

Multiple values are explained here:
CLtL2 is older than the ANSI Common Lisp specification, so in case of doubt look-up the MULTIPLE-VALUE-... operators in CLHS, but the explanation in CLtL2 is much better than CLHS Section 5.1.2.3 VALUES Forms as Places.

macrolyte
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Re: Accessor values question

Post by macrolyte » Fri May 23, 2014 4:25 pm

edgar-rft wrote: The reason is that Common Lisp functions do not handle multiple values by default.
Multiple values are explained here:
CLtL2 is older than the ANSI Common Lisp specification, so in case of doubt look-up the MULTIPLE-VALUE-... operators in CLHS, but the explanation in CLtL2 is much better than CLHS Section 5.1.2.3 VALUES Forms as Places.
Thanks, I've read the section in PCL, it was really good, and will read the others you listed in a bit.
edgar-rft wrote: The error message is misleading. IMO this is is a bug in your Common Lisp implementation. A correct error message should read "The value 3 is not of type LIST" or similar.
Frankly, I have to agree with your opinion. I've run into many situations where the errors returned have given little or incomplete information. I'm also frustrated by its lack of built-in thread support. If you know of a good implementation that plays well with Emacs/Slime and does threads right out the box, let me know. Again, thanks for your help.

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Accessor values question

Post by Goheeca » Sat May 24, 2014 3:49 am

SBCL has good support of threads nowadays.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

macrolyte
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Re: Accessor values question

Post by macrolyte » Sat May 24, 2014 1:25 pm

Hey, Goheeca!
I've been using Cygwin on Windows 7, (don't laugh), for development which doesn't have a port for SBCL. So I went to their site, downloaded the x86 installer, ran it, started SBCL, evaluated *features* and there was absolutely no thread package! So a quick search turned up:
SBCL wrote: Threads are part of the default build on x86[-64] Linux only.

They are also experimentally supported on: x86[-64] Darwin (Mac OS X), x86[-64] FreeBSD, x86 SunOS (Solaris), and PPC Linux. On these platforms threads must be explicitly enabled at build-time, see INSTALL for directions.
It will be a few months before I have FreeBSD up and running again, so I almost gave up until I found this, which is a fork of a fork of SBCL by Anton Kovalenko. I installed, ran it, and of course tested it:

Code: Select all

(make-thread (lambda()(write-line "Hello, world")))

;;; caught STYLE-WARNING:
;   undefined function: MAKE-THREAD
;
; compilation unit finished
;   Undefined function:
;     MAKE-THREAD
pretty verbose. So then I remembered and tried:

Code: Select all

* (sb-thread::make-thread (lambda()(write-line "Hello")))

Hello
#<SB-THREAD:THREAD RUNNING {23E1BB49}>

so I'm not really sure what is going on, but I'm happy so far. I'll probably setup some sort of environment to play around with SBCL. Thanks for this!

BTW, how the heck do I kill the thread?? o.0

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Accessor values question

Post by edgar-rft » Sat May 24, 2014 5:22 pm

macrolyte wrote:... how the heck do I kill the thread?
See the SBCL User Manual, Section 12 Threading.

Clozure CL is also known for running on Windows, see Clozure CL on Windows and Programming with Threads.

A pretty complete overview about the capabilites of the various Common Lisp implementations can be found here:
The document if four years old but 99% still true, except such things like "the SBCL Windows support today is much better than it was in 2010"...

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Accessor values question

Post by Goheeca » Sun May 25, 2014 4:37 am

I'm using Cygwin, too ;). Kovalenko's fork has been merged quite recently. I was using Kovalenko's fork before merging. Despite saying that SBCL is running on Windows experimentally, the unmuffleable initial message Your Kitten of Death awaits has disappeared so it's getting better. In *features* of original SBCL is available :sb-thread. You should install bordeaux-threads to have functions like make-thread at disposal.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Post Reply