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

Pages: 1-

haskell composition

Name: Anonymous 2007-06-19 14:08 ID:sxIr1ztw

how do I compose in haskell two functions when one of the two functions requires more than one parameter?
What I want to do is something like this*:

(defun compose (f g)
    (lambda (&rest x)
        (funcall f (apply g x))))


but the . operator in haskell doesn't work if I want to compose, say, not and >




*or, for the scheme crowd out there

(define (compose f g)
     (lambda x (f (apply g x))))

Name: Anonymous 2007-06-19 14:24 ID:vvjgHPGI

Let's say you want to compose a function of type c->d with a function of type a->b->c

We use the function dot, which we can define thusly:
dot = (.).(.)

Now we can write stuff like:
divisibleBy = (== 0) `dot` mod

Name: Anonymous 2007-06-19 14:52 ID:Heaven

this is why factor is so much better than those other languages.

Name: Anonymous 2007-06-19 14:59 ID:sxIr1ztw

>>2
thanks anon, I'm still trying to wrap my mind around that

Name: Anonymous 2007-06-19 17:17 ID:z1u9R+Kb

>>2
Haskell is insane. Anybody could understand this
(define (compose f g)
     (lambda x (f (apply g x))))

without having even heard of LISP. Yet what the fuck is (.).(.)? Some kind of ASCII art? And what about (== 0) `dot` mod? It looks like some bizarre shell programming.

Name: Anonymous 2007-06-19 17:36 ID:vvjgHPGI

>>5
I expect to be able to understand a piece of code without ever having learnt the syntax. If I can't, it's clearly the language's fault; the designers should have anticipated which languages I am familiar with and designed the syntax accordingly.

OR NOT LOL

Name: Anonymous 2007-06-19 17:48 ID:Heaven

Anybody could understand this ... without having even heard of LISP.
that's true, but this is even easier to understand:
: compose ( f g -- fg ) >r execute r> execute ;

and there's no )))).

Name: Anonymous 2007-06-19 17:49 ID:vvjgHPGI

>>7
Factor fags are the furries of /prog/

Name: Anonymous 2007-06-19 17:51 ID:x5ZbG83c

>>5
you could do [code]dot f g x y = f (g x y)[code]

but all the smart kids write pointfree code

Name: Anonymous 2007-06-19 17:52 ID:Heaven

FUCK I FAIL AT BBCODE

Name: Anonymous 2007-06-19 18:41 ID:cyNyBeoh

>>7
fail.
USING: quotations ;
: compose ( f g -- quot ) [ ] rot dup quotation? [ 1quotation ] unless add swap dup quotation? [ 1quotation ] unless add concat ;

Name: Anonymous 2007-06-19 18:56 ID:jj1cTIiO

point-free? more liek pointless.

Name: Anonymous 2007-06-19 20:59 ID:5OXkHGxv

function compose(f,g)
    return function(...) f(g(...)) end
end

Lua = win

Name: Anonymous 2007-06-19 21:08 ID:5OXkHGxv

FUCK

function compose(f,g)
    return function(...) return f(g(...)) end
end

Name: Anonymous 2007-06-19 21:27 ID:Heaven

#define compose(f,g) int f ## g (int x) { return (int)f(g(x)); }

REAL PROGRAMMERS USE C

Name: Anonymous 2007-06-20 0:48 ID:zGyv9O2T

>>15
you failed it the same way >>7 did.

Name: Anonymous 2007-06-20 1:15 ID:Heaven

>>16
compose(sin,cos)

int foo(int x) {
    return sincos(0);
}

I AM AN EXPERT PROGRAMMER
I AM USING RETURN-BY-NAME-MANGLING

Name: Anonymous 2007-06-20 9:50 ID:d/Q36+To

>>13
Truth. I'll also propose Python (supporting dictionary arguments too):

compose = lambda f, g: lambda *a, **aa: f(g(*a, **aa)

So far and IMO, the clearest(*) languages to implement function composition as seen in this thread are:
1. Lua
2. Scheme
3. Python
4. Common Lisp

(*): A property which I rightfully value for reasons beyond the scope of this post.

Name: Anonymous 2007-06-20 11:10 ID:Heaven

actually, if you just want to combine quotations (http://factorcode.org/responder/help/show-help?topic=quotations) in factor you can just use append (http://factorcode.org/responder/browser/browse?apropos=&word=append&vocab=sequences).

also, the documentation for factor totally owns anything i've seen for any of the other languages mentioned in this thread.

Name: Anonymous 2009-01-14 14:53

NO EXCEPTIONS

Name: Anonymous 2009-02-25 7:43

Print to screen how   to take input   from the user   to enter the   number and compute.

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