defining a named function with explicitly named arguments |
incrementInteger :: Int -> Int incrementInteger n = n + 1
|
incrementInteger[n_Integer] := n + 1;
|
|
defining a named function with lambda expression |
incrementInteger :: Int -> Int incrementInteger = \n -> n + 1
|
incrementInteger = Function[{n}, n + 1];
|
|
defining a function with a function name as one of the
arguments |
Prelude> increment = \x -> x+1 Prelude> double = \x -> 2*x Prelude> thenDouble someFunc = double . someFunc Prelude> thenDouble increment 3 8
|
In[1]:= double = Function[x, 2x]; increment = Function[x, x + 1]; thenDouble[someFunc_] := Composition[double, someFunc];
In[4]:= thenDouble[increment]
Out[4]= Function[x, 2 x]@Function[x, x + 1]
In[5]:= (thenDouble[increment])[3]
Out[5]= 8
|
|
defining a function with locally definded intermediate
functions |
Prelude> :{ Prelude> myFunc1 x = Prelude> let y = sin x Prelude> in cos y Prelude> :} Prelude> myFunc1 pi 1.0
Prelude> :{ Prelude> myFunc2 x = Prelude> cos y Prelude> where Prelude> y = sin x Prelude> :} Prelude> myFunc2 pi 1.0
|
In[1]:= myFunc[x_]:=With[{f=Sin[x]},Cos[f]] myFunc[Pi] Out[2]= 1
|
Alternatively, instead of using With
(for symbolic replacement),
use Module (for lexical scoping) or Block
(for dynamic scoping). |