Hi Ladislav,

> I remember that Jeff submitted something on this, but i lost it somewhere.

Jeff's example:

add2: func [x] [
  loop 1 compose [add 2 (x)]
]


> Request #2: I do'nt think this function is reliable ...

after a small change, your func 'curried works more "reliable":

REBOL 2.2.0.3.1 [MSC 32 bit (Intel)] on win32
Login at 6.12.1999 16:18:41

>> do %curried.r
>> fun: func [a b] [a * 10 + b]
>> cfun: curried :fun [a]
>> dfun: cfun 1
>> dfun 2
== 12
>> fun2: func [a b] [a * 20 - b]
>> efun: curried :fun2 [b]
>> ffun: efun 2
>> ffun 1
== 18
>> dfun 2
== 12
>> source cfun
cfun: func [a][func [b] [func [a b][a * 10 + b] a b]]
>> source dfun
dfun: func [b][func [a b][a * 10 + b] a b]
>> source efun
efun: func [b][func [a] [func [a b][a * 20 - b] a b]]
>> source ffun
ffun: func [a][func [a b][a * 20 - b] a b]
>> source curried
curried: func ["Create curried functions"
    fnc [any-function!] "Function to be curried"
    args [block!] "Arguments of the curried fnc"
    /local formargs restargs nonargs original
][
    formargs: first :fnc
    if not empty? nonargs: difference/only args formargs [
        make error! [script expect-arg curried nonargs]
    ]
    restargs: difference/only formargs args
    ; *** here's the difference ***
    original: append copy reduce [get 'fnc] formargs
    func args reduce ['func restargs original]
]
>>

Regards,

Oliver Schaefer ([EMAIL PROTECTED])


----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, December 03, 1999 1:11 PM
Subject: [REBOL] Curried functions


> Hi, Rebols,
>
> during our discussion with Elan on  preventing code duplication in objects
> it seemed good to have the curried functions.
>
> I remember that Jeff submitted something on this, but i lost it somewhere.
>
> This is an independent solution, trying to be as complete as possible.
>
> Enjoy the expressive power of Rebol - a gem (thanks, Carl 8^)
>
> -Ladislav
>
> Rebol [
>     Title: "Curried"
>     Date: 3/12/1999
>     File: %curried.r
>     Author: "Ladislav Mecir"
>     Email: [EMAIL PROTECTED]
>     ]
>
> curried: func ["Create curried functions"
>     fnc [any-function!] "Function to be curried"
>     args [block!] "Arguments of the curried fnc"
>     /local formargs restargs nonargs original
>     ] [
>     formargs: first :fnc
>     if not empty? nonargs: difference/only args formargs [
>         make error! [script expect-arg curried nonargs]
>         ]
>     restargs: difference/only formargs args
>     original: append copy [fnc] formargs
>     func args reduce ['func restargs original]
>     ]
>
> ;**********end of %curried.r
>
>
> some examples:
>
> >> fun: func [a b] [a * 10 + b]
> >> cfun: curried :fun [c]
> ** Script Error: curried expected nonargs argument of type: none.
> ** Where: make error! compose [script expect-arg curried nonargs]
>
> Request #1: This shows how CURRIED handles errors (argument C is not
> allowed, because it isn't between the formal args of FUN). Can somebody
show
> me, how to MAKE ERROR! so, that I could say which args are incorrect -
those
> in NONARGS).
>
> more examples:
>
> >> cfun: curried :fun [a]
> >> source cfun
> cfun: func [a][func [b] [fnc a b]]
> >> dfun: cfun 1
> >> source dfun
> dfun: func [b][fnc a b]
> >> dfun 2
> == 12
>
> Request #2: I do'nt think this function is reliable (what a pity! - Jeff,
> Bo, anyone?), because when you look at source DFUN you see FNC in there,
but
> FNC is argument of CURRIED and valid only until you evaluate CURRIED for
the
> next time, due to the nonreentrant nature of function evaluation, problems
> with extents, garbage collector ... So Rebol shows exceptional
> expressibility, but worse implementation here...
>
>



Reply via email to