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

PROGRAMMING CHALLENGE

Name: Anonymous 2010-07-16 18:57


This one should be difficult enough for you guys,

Write a function foo that takes a number n and returns a function that takes a number i, and returns n incremented by i.


My submission, in Scheme


(define (foo num)
  (lambda (x) (+ x num)))

Name: Anonymous 2010-07-17 0:23

>>40
No. Supporting nested functions does not necessitate supporting closures.

Name: Anonymous 2010-07-17 0:24

>>41
The GCC nested functions extension support closures, which is what we're talking about.

Name: Anonymous 2010-07-17 0:27

>>42
Well, arguably clang could support closures without supporting nested functions but I don't know how much use that would be.

Name: Anonymous 2010-07-17 0:30

>>43
Oddly enough, Clang supports both Objective-C style blocks (lambda functions and closures) and C++0x lambda functions with closures, but both are implemented through different mechanisms, and both aren't language extensions.

Name: Anonymous 2010-07-17 0:43

>Write a function foo that takes a number n and returns a function that takes a number i, and returns n incremented by i.
a=parseInt(prompt("Enter a number",0));;
eval("function newf(i){return a+i}")
newf(10);

Name: Anonymous 2010-07-17 1:23

>>45
function foo(n){return function(i){return n+i}}
foo(pareInt(prompt('Enter a number', 0)))(10);

Name: Anonymous 2010-07-17 1:35

>>46
You can't reuse that function without declaring it with a name outside function scope. Observe:
function check(){
a=parseInt(prompt("Enter a number",0));;
eval("function newf(i){return a+i}")}
check()
newf(10);//Error: newf is not defined

Name: Anonymous 2010-07-17 1:48


function gen(n)
   return function (i)
      return n + i
   end
end

Name: Anonymous 2010-07-17 1:59

>>48
Only Lua comes close to Lisp in terms of beauty.

Name: Anonymous 2010-07-17 2:33

>>29
It's not C99, it's a GNU extension. You need to give GCC -std=c99 -pedantic to make it complain about non-C99 code.

Name: Anonymous 2010-07-17 2:34

i hate niggers

Name: Anonymous 2010-07-17 2:38

Java can do something like this with nested classes but the effort is not really worth it in most cases.

public class MyThing
{
   abstract class OtherThing
   {
      abstract int add(int x);
   }
   protected int i;

   public MyThing(int a) { i = a; }

   public int add(final int n)
   {
      OtherThing thing = new OtherThing() {
         public int add(int x)
         {
            return n + x;
         }
      };
      return thing.add(i);
   }

   public static void main(String[] args) { }
}


What the hell did I just write?

Name: Anonymous 2010-07-17 2:45

>>52
Why would you do that?
OOP and closures are somewhat equivalent.

A class with a (possibly private) member called i, a constructor which takes i, and an add method which takes n is enough.

Usage would be like:

Adder adder = new Adder(i);
int newValue = adder.Add(n,i);


It's not exactly what OP requested for, but the concepts are somewhat equivalent. It's easier to implement OO systems if you already have closures and macros, but it's harder to implement closures if you only have classes, but aside from that the functionality they present is somewhat equivalent.

Name: Anonymous 2010-07-17 3:03

>>53
I was trying to go for a "nested functions" feel and this was the only way I could think of achieving that.

I suppose I could the Java Method class in some way too but that would be a ridiculous amount of work compared to the simplicity of the request.

Name: Anonymous 2010-07-17 4:08

Any solution which manipulates function pointers is very messy.
(return *func_pointer)(execute_from) vs setting global variable x=n; and using standard function calls

#include <stdio.h>
#include <stdlib.h>
int n=0;
int getn(i){return n+i;}
int main(int agrc,char**argv){
n=10;printf("%i",getn(6));
return 0;
}

Name: Anonymous 2010-07-17 4:18

>>51
Good for you.

Name: Anonymous 2010-07-17 5:03

>>55
Any solution involving globals isn't a real solution as you can't just "make as many functions/closures" as you'd like. You can only "generate" one such function. "Making" another invalidates the previous one.

Name: Anonymous 2010-07-17 5:05

JESUS FUCK PEOPLE WHAT THE FUCK IS WRONG WITH THIS THREAD

Use the motherfucking
  __                      _          __
 | _|   ___    ___     __| |   ___  |_ |
 | |   / __|  / _ \   / _` |  / _ \  | |
 | |  | (__  | (_) | | (_| | |  __/  | |
 | |   \___|  \___/   \__,_|  \___|  | |
 |__|                               |__|
tags in the future.


Sheesh.

Name: Anonymous 2010-07-17 5:28

GolfScript:
~{+}+
Example:
$ echo 6 | ruby golfscript.rb funct.gs
{6 +}

Name: Anonymous 2010-07-17 7:16

>>58
when i'll post Python code i'll take this into account.

Name: Anonymous 2010-07-17 7:23

>>58
Number of Posts with code tags:   18
Number of posts without code tags: 5
78% isn't bad for /prog/

Name: Anonymous 2010-07-17 7:31

>>61
That's like saying that losing a finger is not bad since you still have nine left.

Name: Anonymous 2010-07-17 7:48

>>62
Not really

Name: Anonymous 2010-07-17 7:53

>>61
78% isn't bad for summer /prog/, but I don't think that's really a good enough standard to strive for.

Name: Anonymous 2010-07-17 9:44

>>63
Oh fine.
That's like saying that losing two and one-fifth of a finger is not bad since you still have seven and four-fifths left.

Name: Anonymous 2010-07-17 9:45


foo :: (Num a) => a -> (a -> a)
foo n = \x + n

Name: Anonymous 2010-07-17 9:46

>>65
Percentage pedantry aside, the whole analogy was flawed to begin with.

Name: Anonymous 2010-07-17 9:47

>>66
parse error

Name: Anonymous 2010-07-17 9:50

>>68
right, should be

foo :: (Num a) => a -> (a -> a)
foo n = \x -> x + n

started with haskell just a few days ago

Name: Anonymous 2010-07-17 9:50

>>66
See >>15.

Name: Anonymous 2010-07-17 9:51

>>70
ah. makes sense. ttly forgot about currying

Name: Anonymous 2010-07-17 12:36

>>71
* ttly forgot about vowels

Name: Anonymous 2010-07-17 13:45

>>72
* ttlly forgot the other l

Name: Anonymous 2010-07-17 13:50

Bah, I can't come up with a cool and portable solution for C.

Name: Anonymous 2010-07-17 14:01

function foo(n) {
    return function(i) {
        return n + i;
    }
}

Name: Anonymous 2010-07-17 14:02

ffffffffffffffffforgot a semicolon.

Name: Anonymous 2010-07-18 2:22

>>4
1) would work, except it's more complicated than you make it seem; for instance malloc'd memory is execution-protected on modern operating systems. Honestly to make it portable, your best bet is probably to compile it with llvm. You still have to manage the memory containing the closure though, so it's still not like a closure in a typical HLL.

2) does not work because your funcall macro can't be passed around as a function pointer. there's no real way to do it other than return a struct (the closing environment) and pass in that struct in the function call.

In other words it's certainly not possible in standard C, but possible with runtime code generation or compiler extensions.

Name: Anonymous 2010-07-18 4:18

>>6
>>11
int main()
{
    int (*my)(int) = foo(1);
    int (*mi)(int) = foo(2);
   
    printf(" my: %d \n mi: %d\n", my(0), mi(0));

    return 0;
}

my: 2
 mi: 2

EXPERT WINDOWS PROGRAMMERS

Name: Anonymous 2010-07-18 4:21

>>76
You wouldn't need any semicolons for that in Perl but you'd use them anyway.

For some reason I find keeping it on a single line helps me remember the semicolon after curlies. It's also the only excuse I sympathize with for instancing typedefs with their definitions.

Name: Anonymous 2010-07-18 5:32

perl:
sub foo{eval"sub{@_+pop}"}

javascript (1.8+):
foo = function(n) function(i) n + i;

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