|
|
Hey anyone out there.
I think most of you know about, or just heard of LISP in your life at least once.
Nowadays I'm using a LISP dialect scheme to understand this Functional Programming Paradigm.
I really can say that after years of programming with object orientation basis. it is really hard to get used to the main idea of f.p.
Where,
every function has a value, and every value is a function.
Scheme forces you to use recursion, (which is the best programming method ever I think)
you really have not much assignments, 'cos everything is a function.
And here comes the most exciting part;
have you ever seen a function whose return value is a new function?
or have you ever seen a function which has function arguments?
ıt really blows your mind up.
| roguehider wrote: | have you ever seen a function whose return value is a new function?
or have you ever seen a function which has function arguments? |
Yes. Both are possible in both C and C++, especially C++.
| Indi wrote: | | roguehider wrote: | have you ever seen a function whose return value is a new function?
or have you ever seen a function which has function arguments? |
Yes. Both are possible in both C and C++, especially C++. |
can you show me some sample code?
I don't really get it?
have you got anything like?
function f(x){
___int g(y){
______i++;
___};
}
I mean the basic. syntax might be different
LISP functional programming is vital strength over C/C++, howover python/ruby comes very near by "everything is object ". Most of things are possible in both LISP and C/C++ but Lisp version of code is less wordy and efficient. However a lot of parentheses sometime criticize as "Lots of Irritating Superfluous Parentheses", "Let's Insert Some Parentheses", or "Long Irritating Series of Parentheses"
| roguehider wrote: | | Indi wrote: | | roguehider wrote: | have you ever seen a function whose return value is a new function?
or have you ever seen a function which has function arguments? |
Yes. Both are possible in both C and C++, especially C++. |
can you show me some sample code?
I don't really get it?
have you got anything like?
function f(x){
___int g(y){
______i++;
___};
}
I mean the basic. syntax might be different |
In C? It's been a long time since i've done C. But to pass and return functions you use function pointers. The base syntax is hideous, though:
| Code: | #include <stdio.h>
#include <stdlib.h>
// First, two ordinary functions
int add(int a, int b)
{
return a + b;
}
int multipy(int a, int b)
{
return a * b;
}
// This function takes a function as an argument
int do_operation(int (*func)(int), int a, int b)
{
return func(a, b);
}
int main(void)
{
// Prints 15 (5 + 10)
printf("Passing add() function as a variable: %d\n",
do_operation(&add, 5, 10);
// Prints 50 (5 * 10)
printf("Passing multiply() function as a variable: %d\n",
do_operation(&multiply, 5, 10);
return EXIT_SUCCESS;
} |
That code should compile, but i haven't tested it.
Typedefs make the syntax prettier:
| Code: | #include <stdio.h>
#include <stdlib.h>
typedef int (*operation)(int, int);
// First, two ordinary functions
int add(int a, int b)
{
return a + b;
}
int multipy(int a, int b)
{
return a * b;
}
// This function takes a function as an argument
int do_operation(operation func, int a, int b)
{
return func(a, b);
}
int main(void)
{
// Prints 15 (5 + 10)
printf("Passing add() function as a variable: %d\n",
do_operation(&add, 5, 10);
// Prints 50 (5 * 10)
printf("Passing multiply() function as a variable: %d\n",
do_operation(&multiply, 5, 10);
return EXIT_SUCCESS;
} |
In C++, you have function objects, which are a whole world more powerful, and safer:
| Code: | #include <functional>
#include <iostream>
// First, two ordinary functions
int add(int a, int b)
{
return a + b;
}
int multipy(int a, int b)
{
return a * b;
}
// This function takes a function as an argument
int do_operation(std::binary_function<int, int, int>& func, int a, int b)
{
return func(a, b);
}
int main()
{
// Prints 15 (5 + 10)
std::cout << "Passing add() function as a variable: " <<
do_operation(std::ptr_fun(add), 5, 10) << '\n';
// Prints 50 (5 * 10)
std::cout << "Passing multiply() function as a variable:" <<
do_operation(std::ptr_fun(multiply), 5, 10) << '\n';
return 0;
} |
That's just doing the same thing that the C code does. But function objetcs in C++ can do much, much more. You can have a function object that counts the number of times its function is called, or one that writes warnings to log files for deprecated functions. Or one that calls its function asynchronously. The possibilites are endless. Function objects are also full objects, so you can derive from them or create them by derivation.
That's just passing them as parameters, but of course, you can return them, too. Want to see sample code?
You can also return from a function and point to another function in C/C++ using buffer overflows. A function has a return address and you can easily overwrite it with the address of another function.
Of course most of the time it's not exactly a legal thing to do.
| Liu wrote: | You can also return from a function and point to another function in C/C++ using buffer overflows. A function has a return address and you can easily overwrite it with the address of another function.
Of course most of the time it's not exactly a legal thing to do. |
Well, yeah, but that's techncally not C++. That's using C++ to hack, which is not portable. You can do that in any language.
| Indi wrote: | | roguehider wrote: | | Indi wrote: | | roguehider wrote: | have you ever seen a function whose return value is a new function?
or have you ever seen a function which has function arguments? |
Yes. Both are possible in both C and C++, especially C++. |
can you show me some sample code?
I don't really get it?
have you got anything like?
function f(x){
___int g(y){
______i++;
___};
}
I mean the basic. syntax might be different |
In C? It's been a long time since i've done C. But to pass and return functions you use function pointers. The base syntax is hideous, though:
| Code: | #include <stdio.h>
#include <stdlib.h>
// First, two ordinary functions
int add(int a, int b)
{
return a + b;
}
int multipy(int a, int b)
{
return a * b;
}
// This function takes a function as an argument
int do_operation(int (*func)(int), int a, int b)
{
return func(a, b);
}
int main(void)
{
// Prints 15 (5 + 10)
printf("Passing add() function as a variable: %d\n",
do_operation(&add, 5, 10);
// Prints 50 (5 * 10)
printf("Passing multiply() function as a variable: %d\n",
do_operation(&multiply, 5, 10);
return EXIT_SUCCESS;
} |
That code should compile, but i haven't tested it.
Typedefs make the syntax prettier:
| Code: | #include <stdio.h>
#include <stdlib.h>
typedef int (*operation)(int, int);
// First, two ordinary functions
int add(int a, int b)
{
return a + b;
}
int multipy(int a, int b)
{
return a * b;
}
// This function takes a function as an argument
int do_operation(operation func, int a, int b)
{
return func(a, b);
}
int main(void)
{
// Prints 15 (5 + 10)
printf("Passing add() function as a variable: %d\n",
do_operation(&add, 5, 10);
// Prints 50 (5 * 10)
printf("Passing multiply() function as a variable: %d\n",
do_operation(&multiply, 5, 10);
return EXIT_SUCCESS;
} |
In C++, you have function objects, which are a whole world more powerful, and safer:
| Code: | #include <functional>
#include <iostream>
// First, two ordinary functions
int add(int a, int b)
{
return a + b;
}
int multipy(int a, int b)
{
return a * b;
}
// This function takes a function as an argument
int do_operation(std::binary_function<int, int, int>& func, int a, int b)
{
return func(a, b);
}
int main()
{
// Prints 15 (5 + 10)
std::cout << "Passing add() function as a variable: " <<
do_operation(std::ptr_fun(add), 5, 10) << '\n';
// Prints 50 (5 * 10)
std::cout << "Passing multiply() function as a variable:" <<
do_operation(std::ptr_fun(multiply), 5, 10) << '\n';
return 0;
} |
That's just doing the same thing that the C code does. But function objetcs in C++ can do much, much more. You can have a function object that counts the number of times its function is called, or one that writes warnings to log files for deprecated functions. Or one that calls its function asynchronously. The possibilites are endless. Function objects are also full objects, so you can derive from them or create them by derivation.
That's just passing them as parameters, but of course, you can return them, too. Want to see sample code? |
Aha, The pointer fun in C. I really don't know much on pointers.
Mostly I'm coding in Java, and now this made think "Oh! java really sucks!"
In java I had done something like your c++ version of code.
Firstly defining an interface for binary operations on ints. and then it somehow looks just the same method you used in your example.
Thank you, for your precious time and code also 
well i have heard about this functional programming but can some body tell me what is this actually
yes you wrote smoe code in c, c++ but in common lisp it would be:
| Code: | (defun lfun (fn)
(funcall fn 2 3))
(lfun '+)
=>5
(lfun '*)
=>6
|
| Code: | (defun lsum (lst fn)
(let ((s 0))
(dolist (e lst s)
(incf s (funcall fn e)))))
(defun sq (x)
(* x x))
(lsum '(1 2 3) #'sq)
=>14
|
lsum returns sum, of values returned by sq, for 1,2 and 3.
1*1+2*2+3*3= 1 + 4 + 9 = 14
yes of course you can write it with c or c++, but ... ;P
one end thing ,there are different programming languages, and different people write their programs in different languages. Don't argue which language is better than ... 
Never done any LISP/Scheme although I've looked at some of the code, I've done some Haskell before though - that was pretty fun :p
| wilan wrote: | | I've done some Haskell before though - that was pretty fun :p |
Haskell indeed is fun. One of the nicest languages to program in. But sometimes...
I once tried to make a Haskell program to solve sudoku's. While still wrestling to debug my 500+ lines of haskell, I came across this solution:
| Code: |
import List
type T = (Int,Int) -> [Int]
main = do
s <- getContents
putStr $ unlines $ map disp $ solve [input s]
solve :: [T] -> [T]
solve s = foldr search s idx where
search p l = [mark (p,n) s | s <- l, n <- s p]
mark :: ((Int,Int),Int) -> T -> T
mark (p@(i,j),n) s q@(x,y) =
if p==q then [n] else
if x==i || y==j || e x i && e y j then delete n $ s q else s q
where e a b = div (a-1) 3==div (b-1) 3
disp :: T -> String
disp s = unlines [unwords [show $ head $ s (i,j) | j <- [1..9]] | i <- [1..9]]
input :: String -> T
input s = foldr mark (const [1..9]) $
[(p,n) | (p,n) <- zip idx $ map read $ lines s >>= words, n>0]
idx :: [(Int,Int)]
idx = [(i,j) | i <- [1..9], j <- [1..9]]
|
(http://web.math.unifi.it/users/maggesi/haskell_sudoku_solver.html)
I didn't know whether to laugh or to cry Never finished my own solution.
|