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

Pages: 1-

Partial application of arguments

Name: Anonymous 2013-07-29 16:52

I am trying to figure this out in C++, Haskell and Common Lisp, for anyone who cares. How'd you write a macro that does the following:


(foo (f _ y _))
==> (lambda (x z) (f x y z))


Same thing for Haskell, can it be done? What's the proper way to do this? What of C++?

Name: Anonymous 2013-07-29 17:05

C++ is shit

Name: Anonymous 2013-07-29 17:07

C++ is almost as bad as jeggings.

Name: Anonymous 2013-07-29 18:16

For the longest time you were just that sad little annoying person on 4chan who is always trying to hang out with me. You have liked me for YEARS since you were a sad little mentally retarded kid at the apartments. I have never liked you ever and all your attempts to go out with me have made me want to throw up in the past. I have actually gagged at the thought of going on a date with you. Gagged. I have been dating my fiance for 4 years now and the entire time you have been making these really pathetic attempts to flirt or get at me, probably jerking off by yourself since no girl would want to fuck you unless she was as ugly and retarded as your family is. You are an unpleasant bug splatter under my heel, an ugly stain on the shirt of life, and have a face only a mother could love provided she was blind. You are too stupid to understand that I have a man and don't need a retard in a padded helmet. I just got tired of seeing your constant limp-dicked attempt at hanging out with me. Please do me a favor and leave me and my fiance alone. You hated him just because I was with him and never gave you the time of day. Please do me a favor, tonight when you are thinking about what happened now and tending to your emotional wounds, picking up the broken pieces of what little confidence you don't have, picture me with my fiance and please use your imagination. Its as close to having me as you will ever get. Have a horrible day and die, Cunt."

Name: Anonymous 2013-07-29 18:34

>>4
U MAD?

Name: Anonymous 2013-07-29 19:27

United as one, divided by zero. We are Anonymous, We are legion, We do not forgive. We used to be actually cool, and worth the laugh and the reminder not to take things too seriously. But in our haste to remind others of their canny ability to ignore reality, we've expended all this energy, more energy than they do avoiding reality. So now we're kindof a sad mess of dumbasses still clinging to an outdated manifesto that may as well be the instructions on someone's VCR manual. We're sad. Sad sad people. We eat Doritos for breakfast, lunch and dinner. We write minimal annoying scripts that do fuckall except annoy people for 12 seconds. We're quick to brag about our endeavors because what press we've recieved has given us a false sense of validity. But don't fool yourselves. We're a bunch of sad, undersexed, testosterone-filled highschool dropouts who work fast food, live off our parents and smoke too much weed. Move along now. We've hit the snooze button on our 15 minutes of fame. Our voices are silent.

Name: Anonymous 2013-07-29 20:15

ALL THREADS WILL BE REPORTED AND DELETED. NO EXCEPTIONS.

Name: Anonymous 2013-07-29 23:02

You might be able to engineer something in C++ using templates, but it will be really ugly looking and will only work for the arities you define.

Here's a way in scheme that works well.


(define (curry f . args)
  (lambda args2
    (apply f (append args args2))))

((curry + 11 22) 33) ==> 66


Implicit currying is nice, but breaks varadic arguments.

Name: Anonymous 2013-07-29 23:07

>>6
testosterone-filled
I don' fucking think so

Name: Anonymous 2013-07-29 23:09

>>1
This is an example of problem, that could have been easily done in assembly (by generating machine code), but almost impossible to do in C/C++.

Moral: C/C++ isn't a substitute for proper assembly and we need more love level, but still portable language.

Name: Anonymous 2013-07-29 23:12

Name: Anonymous 2013-07-29 23:16

>>11
boost::lambda::lambda_functor< boost::lambda::lambda_functor_base< boost::lambda::bitwise_action< boost::lambda::leftshift_action>,boost::tuples::tuple< boost::lambda::lambda_functor< boost::lambda::lambda_functor_base< boost::lambda::bitwise_action< boost::lambda::leftshift_action>,boost::tuples::tuple< std::ostream&,boost::lambda::lambda_functor< boost::lambda::placeholder<1> >,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type> > >,const char,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type> > >::operator

Name: Anonymous 2013-07-30 1:12

PARTIALLY APPLY MY ANUS

Name: Anonymous 2013-07-30 5:20

>>8
Yeah but let's say that the function in use is not associate? (i.e. the order of the arguments matters). In that case I can't use your function curry because I might want to pass the second argument of the three (as in my OP).

Name: Anonymous 2013-07-30 5:21

>>14
associative* sorry

Name: Anonymous 2013-07-30 5:23

>>14,15
commutative* :(

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-07-30 6:10

>>8
As if dozens of parentheses are any less ugly?

Name: Anonymous 2013-07-30 6:48

>>17
I think lisp syntax is most beautiful.

Name: Anonymous 2013-07-30 7:10

>>1

In haskell you can do this in several ways.

(1) Explicitly naming the holes, wrap you expression in a lambda:


(\a b -> f a 1 b)


This is the most explicit version.

(2)Next you can use combinators:


flip :: (a -> b -> c) -> b -> a -> c
flip f a b = f b a

rot3 :: (a -> b -> c -> d) -> c -> a -> b -> d
rot3 f c a b = f a b c


This sucks a bit, for the example of 1, we need to do:

flip (flip f 1)


Which isn't very readable.

(3)The last thing is the most advanced, but I don't see any advantage in it. You can use quasiquoters to create a little language, which does the currying for you:

[curry|f _ 1 _]



(3) and (2) are very similar, I don't see (3) has an advantage over (1), even the amount of strokes I have to type are similar. Although with longer functions (1) can get more tedious, but in haskell functions never reach more than 4/5 arguments.

Name: Anonymous 2013-07-30 9:44

>>19
I have no idea what (3) is, but I'm a newbie. I thought the [ | ] syntax was for lists, was in [a + b | (a, b) <- zip fib (tail fib)]

I realized however that your flip flip approach is basically a hidden permutation of three arguments, namely the permutation (1 2). (which 'flip's the first and second argument).
If you don't know what I'm talking about see this http://en.wikipedia.org/wiki/Cyclic_permutation

Would it be possible to write a generalized flip, as in,

Suppose f is of type a -> b -> c -> d
Then genflip would,
genflip (1, 2) 3 f -- evaluates to a function of type
-- b -> a -> c -> d
-- note the second argument to genflip is the number of arguments for f


Though that's probably nonsense and can't be done.

You can however do this in common lisp with a macro. I'm not sure if it can be done for lists coming from other sources (i.e. input).

Name: Anonymous 2013-07-30 10:43

>>20

Unfortunately that is not possible with normal haskell. But you can do this in template haskell, which is a very crude form of macro's. (Scheme's macros are superior)  That is what the approach in (3) is too. It is a macro, you call then. (Calling convention: [macro|body|]) or you can also call them as $(gFlip 1 2 3 f). Calling convention is like a normal function $(function <parameters>). The function here is a real function emitting haskell AST.

And it probably can be done in a dependent language with pi types, but haskell isn't dependently typed and it is not easy to program in those languages. It means that the type system has a special type, which accepts an type as argument:

f: PI(x: A),B(x) is a function, where the type of B is dependent on the type of A.

if B(x) = const C x then f becomes PI(x : A), const C x and reduces to A -> C.

With this we could create a function flipT which accepts a natural and throws out the correct type of B for a general flip function. flipT should look something like this (pseudocode): 

flipT 1 2 = (a -> b -> c -> d) -> b -> a -> c -> d
flipT 3 1 = (a -> b -> c -> d) -> c -> b -> a -> d
etc.


But this is only half of the puzzle. We didn't add the type of f . Thus flipT would be a function with two inputs flipT, because we need to know the arity of f in advance and don't like to rely on the user for it's input. It think it will be rather difficult, but interesting.

Name: Anonymous 2013-07-30 11:26

>>17
By ugliness, I mean the ratio of (code size)/(thought). In C++ you'll have pages of templates for different arities, as done by >>12.

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