Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

hai prog: multiple return values

Name: Anonymous 2009-10-02 19:31

Hello

I know one can use pass-by-reference for getting multiple values back from a function, but is there a language that allows you to actually define multiple outputs seperate from the parameter list for super neatness.

I was thinking it would be super-cool to have an auto-defined struct based on the function return values. so in some pseudo-language it would be like...


def splitstring  string<s char<sep string>a string>b

s = "hello world"

splitstring s ' '

printf(splitstring.a)
printf(splitstring.b)


would that be totally kawaii aewsome?

obviously each thread would have its only instances of these auto-structs.

Name: Anonymous 2009-10-02 19:46


class Splitstring {

private String a;
private String b;
... repeat until z

public Splitscreen(String s, char del) {
...
}

public String a() {
return a;
}

public String b() {
return b;
}

... repeat until z

public static void main(String[] args) {
String s = "hello world";
Splitstring split = new Splitscreen(s, " ");

System.out.print(s.a);
}

}

Name: Anonymous 2009-10-02 19:47

>>2
woops, should be System.out.print(split.a);

Name: Anonymous 2009-10-02 19:49

>>2
oh yes that's much neater than writing one line

Name: Anonymous 2009-10-02 19:58

>>1
Is this the stupidity /prog/ has been reduced to nowadays? For the fifth or so time this year, /PROG/ IS DYING

Name: Anonymous 2009-10-02 20:09


>>> def get_two_values():
    return 1,2

>>> print get_two_values()
(1, 2)
>>> a,b = get_two_values()
>>> a
1
>>> b
2
>>>

Name: Anonymous 2009-10-02 20:09

>>5
IHBT

Name: Anonymous 2009-10-02 20:37

lua

Name: Anonymous 2009-10-02 20:41

>>5
I see all sorts of folks screaming OMG OMG /prog/ is dying.
One, it's always sucked
Two, it's only as good as you make it.

QED: quit your fucking whining and contribute instead of crying like a girl.

Name: Anonymous 2009-10-02 20:51

See the python example above, which has built in tuple packing/unpacking with the comma operator. Even sepplesox has this with tie().

Name: Anonymous 2009-10-02 20:55

Actually most high-level languages have exactly what you want; split() in almost any scripting language returns a list which you index like an array. You just need to get away from sepples and java/.net. People tell you they're high level languages; they're lying.

Name: Anonymous 2009-10-02 20:59

I just reread op and realized how stupid it is. Have I been trolled?

Fuck you prog, god damnit.

Name: Anonymous 2009-10-02 21:02

>>8
lua
/thread

Name: Anonymous 2009-10-02 21:26

I don't even know what >>1-chan is talking about, so here's some ded dof to tide you over:

Prelude> let foo x y = (div x y, mod x y)
Prelude> :t foo
foo :: (Integral a) => a -> a -> (a, a)
Prelude> :t divMod
divMod :: (Integral a) => a -> a -> (a, a)
Prelude> error "hax my anus"
*** Exception: hax m*** Exception: Stack overflow

Name: Anonymous 2009-10-02 21:28

(multiple-value-bind (a b) (split-string "hello world")
  (print a)
  (print b))


Implementing SPLIT-STRING left as an exercise for the reader.

Name: Anonymous 2009-10-02 22:35

>>1
an auto-defined struct

You could return a void pointer to memory containing:

a short holding the number of pointers n
n pointers
x bytes, referenced by your pointers.

Is this acceptable?

Name: Anonymous 2009-10-02 22:49

>>1
>>15 knows where's it at. Lisp has multiple return values, and much more.

A somewhat wasteful implementation of split-string would be:

(defun split-string (s)
  (values-list (split-sequence s :remove-empty-subseqs t)))

or one could just use destructuring-bind. (SPLIT-SEQUENCE is a tiny CL library.)

Name: Anonymous 2009-10-03 0:00

Every language in history has multiple return values, and much more.
fify

Name: Anonymous 2009-10-03 0:12

>>18
Yes, you can emulate multiple return values using various conventions, but there's huge usability differences between C's out parameters, or returning a pointer to an array/structure/list and Lisp's multiple return values. The first is just a natural consequence of how pointers work, and this allows you to basically get that functionality, while the second is a well-supported part of the language which leads to completly different idioms in writing code. I could explain how this works in detail, but it's best to just see for yourself.

Name: Anonymous 2009-10-04 19:50

>>19
Yes, you can emulate tuple packing/unpacking using various conventions, but there's huge usability differences between Lisp's multiple-value-bind, and Python's tuples. The first is just a natural consequence of how bindings work, and this allows you to basically get that functionality, while the second is a well-supported part of the language which leads to completly different idioms in writing code. I could explain how this works in detail, but it's best to just see for yourself.

Lisp sucks. I've never met a Lisp user who wasn't incredibly pretentious.

Name: Anonymous 2009-10-04 20:00

($x, $y, $z) = &x();
print "$x, $y, $z\n";

sub x()
{
    return(1, 2, 3);
}

Lisp is for faggots.
Perl is a REAL man's high level language.
[u][b]Accept no substitutes.[b][/u]

Name: Anonymous 2009-10-04 20:37

>>21
ACCEPT NO BBCODE!

Name: Anonymous 2009-10-04 21:07

>>1
This is a bad idea. It will only confuse things when real programs are to be written. Perhaps it would be a good addition to a toy language, but not a real one. When you call a function, you want to know what the fuck it's returning. Also, if you have some function return two values in (with your proposed idea), then what happens if one of the values depends on the other? It makes it hard as fuck to debug.
It may look like this:

f(x)
{
   int a
   modify a
   IF WE'RE RETURNING FIRST RETURN VALUE:
      return a

   b = modified a
   IF WE'RE RETURNING SECOND RETURN VALUE:
      return b
}

One can imagine how hard it would be to debug this when the function gets large. So it's a bad idea because people will write large, unclear function and code will be harder to debug.

Name: Anonymous 2009-10-04 21:09

>>23
large, unclear function
large, unclear functions
fixed

Name: Anonymous 2009-10-04 22:38

! {
splitstring ->
    stef(a,b;s,sep)
    staðvær i = 1, l := \lengd s
    stofn
        meðan i <= l lykkja
            ef s[i] = sep þá
                a := strdup(;s,1,i-1),
                b := strdup(;s,i+1,l),
                skila i,
            eflok,
        lykkjulok,
        a := strdup(;s,1,l),
        b := "",
        skila 0
    stofnlok
strdup ->
    stef(;s,from,to)
    staðvær r, i := 1, l
    stofn
        l := to - from + 1,
        l > 0 eða skila "",
        r := \strengur l,
        meðan i <= l lykkja
            r[i] = s[from+i-1],
        lykkjulok,
        r
    stofnlok
}


This probably doesn't work. Also, I should really find something better to do.

Name: Anonymous 2009-10-04 23:14

>>25
It obviously won't work without
*
"GRUNNUR"
;

Name: Anonymous 2009-10-05 1:14

>>20
Lisp sucks. I've never met a Lisp user who wasn't incredibly pretentious.
Typical lower-class sentiment.

Name: Anonymous 2009-10-05 3:05

we are hopefully all programmers here on /prog/.
therefore knowing a programming language by no means makes you upper-class or better than anybody.

Name: Anonymous 2009-10-05 3:06

>>28
forgot to reference >>27 in my post

Name: Anonymous 2009-10-05 3:12

WTF is wrong with this thread? Java does this already. Objects have multiple types within them so... you can return an object that has as many variables contained within it that you want.

Name: Anonymous 2009-10-05 3:28

>>19
refer to >>21
holy shit! an imperative language just happens to be able to do it more elegantly and with less effort than your lisp wankery.

Name: Anonymous 2009-10-05 3:32

return(1, 2, 3);
(values 1 2 3)


Wut.

Name: Anonymous 2009-10-05 3:46

>>28
Lisp.

Name: Anonymous 2009-10-05 3:58

>>31
huh? multiple-value-bind and destructuring-bind can do the same things as in that example, only in a cleaner manner. You can also do many other manipulations with multiple return values that are much harder in other languages.

I have little interest to argue with ignoramuses today. Go read a book or stay as you are.

Name: Anonymous 2009-10-05 4:03

>>33
Allow me to finish your sentence for you: is a joke.
>>34
multiple-value-bind and destructuring-bind can do the same things as in that example, only in a cleaner manner
HAR HAR HAR HAR HAR HAR HAR. Very funny. I almost spit coffee onto my monitor.

You know, you're sounding disturbingly like FV. I'm pretty sure he doesn't know Lisp though.

Name: Anonymous 2009-10-05 4:04

>>35
0/10

Name: Anonymous 2009-10-05 4:11

>>36
No, Lisper.
You are the Trolls.
And if you're being sincere then doh ho ho, you're the programming world's equivalent to Apple fanatics.

Name: Anonymous 2009-10-05 4:43

>>37
3/10

Name: Anonymous 2009-10-05 4:52

>>37
2/10

Name: Anonymous 2009-10-05 5:01

>>39
0/11

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List