Functional Overload Equivalent

What approach should I use in functional programming to overload the method (provide one or more different signatures with the same function name).

I am new to functional programming, so at the moment I'm really not hugging this.

For example, in C #, I will have the following:

public int LowestCommonMultiple(int a, int b)
{
    return (a * b) / GreatestCommonFactor(a, b); // details ommited
}

public int LowestCommonMultiple(List<int> integers)
{
    int commonMultiple = integers[0];
    foreach(var integer in integers)
    {
        commonMultiple = LowestCommonMultiple(commonMultiple, i);
    }
    return commonMultiple;
}

Thank,

EDIT: I don't need an answer in C #, my question is a more functional pargadim question, for example, consider it in Haskell. I assume method overloading is not an option.

+3
source share
4 answers

Haskell . #, #, *.

, , Integer, Bool, :

class Foo a where
    foo :: a -> String

instance Foo Integer where
    foo n = show (n+42)

instance Foo Bool where
    foo b = if b then "Hello" else "Goodbye"

, , foo .

*Main> :t foo
foo :: Foo a => a -> String

, a, foo.

*Main> foo 1295
"1337"
*Main> foo False
"Goodbye"

, , .

*Main> foo "Hello"

<interactive>:1:1:
    No instance for (Foo [Char])
      arising from a use of `foo'
    Possible fix: add an instance declaration for (Foo [Char])
    In the expression: foo "Hello"
    In an equation for `it': it = foo "Hello"

, , Haskell. , lcm .

*Main> :t lcm
lcm :: Integral a => a -> a -> a

, , Integral. , Int, Int, Integer , Int32, Int64 ..

foldl1' lcm, , , ​​ .

* -, , . . , , #. ; Monad - , , .

+2

, , , -, (, ), -. Javascript, , , , javascript .

// a: array or integer
// b: optional integer when a is integer
function LowestCommonMultiple(a, b)
{
    // could also test for presence of b, other validation checking probably good
    if (a instanceof Array) {
       // array code
    } else {
       return (a * b) / GreatestCommonFactor(a, b); // details ommited
    }
}

.

0

, / OO, .

SML ( ML) Scheme/Lisp, . , SML, ( , , ). SML (, C/++/JAVA ..), , , - C C-. , , / ( ).

( , /), ( ) .

0
source

you can write any number of signatures with one amendment - you cannot have 2 signatures with the same types of parameters. Each signature can have a return type

void SomeMethor(){...}
string SomeMethod(string s){...}
object SomeMethod(string s, int i){...}
-1
source

All Articles