|
|
|
|
![]() ![]() |
Mar 26 2005, 08:21 PM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 408 Joined: 7-December 04 From: Nova Scoita, Canada Member No.: 2,604 |
I want a thread where I can see how to solve a Varity of programming problems in many languages. The idea is so I can read short examples to become fluid in many languages. Both reinforcing what I know and learning new stuff. The problems should have a short solution and be solvable in many different languages. Web applications are alright but make some reference on how the code is used (e.g. CGI). The poster can provide the problem and as many different solutions as he likes. For example:
Problem 1. Write a function to compute a factorial. Solution Soln. (problem 1.) (Haskell) CODE fact 1=1 fact 0=1 fact x=fact(x-1) Soln. (problem 1.) (MATLAB) CODE Function y=fact(x)
if (y==1) |(y==0) y=1 else y=x*fact(x-1) end |
|
|
|
Mar 28 2005, 07:09 AM
Post
#2
|
|
|
Neurotical Squirrel ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 590 Joined: 4-November 04 From: Novi Sad, Vojvodina Member No.: 2,127 |
As I can't think of any simple problem (I'm at work, and cracking my head with different problems
CODE Function Fact(ByVal x As Long) As Long If x < 1 Then Fact = 1 Else Fact = x * Fact(x - 1) End If End Function This is a great idea, and an opportunity for all of us to learn something new... |
|
|
|
Mar 28 2005, 10:23 PM
Post
#3
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 408 Joined: 7-December 04 From: Nova Scoita, Canada Member No.: 2,604 |
This is how I think you are suppose to do it in lisp
CODE (defun fact (x) (if (x>1) (fact (- x 1)) (1))) Which is in agreement of what I read from the primmer: http://mypage.iu.edu/~colallen/lp/lp.html Unfortunately the interpreter/compiler I downloaded from: http://clisp.cons.org/ http://sourceforge.net/project/showfiles.php?group_id=1355 http://prdownloads.sourceforge.net/clisp/c...32.zip?download seems to be an implementation of clisp that does not know defun. I reported the problem as a bug and hope to get some feedback. Problem 2. Write a function to perform a binary search on an array: Speeking of seeing a problem solved in more then one language, check out this ”hello world” page: http://www2.latech.edu/~acm/HelloWorld.shtml |
|
|
|
Mar 29 2005, 01:01 AM
Post
#4
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 408 Joined: 7-December 04 From: Nova Scoita, Canada Member No.: 2,604 |
I got my lisp interpreter to work and the correct code is:
CODE (defun fact (x) (if (> x 1) (fact (- x 1)) 1)) In my pervious try I made to mistakes. (x>1) should be (> x 1) because in lisp the function or the operation must appear first in the list followed by the arguments. My second mistake was to type (1) instead of 1. When lisp evaluates a list it assumes the first element of the list is a function or operator. 1 is a number not a function or an operator. I also made one minor mistake in my MATLAB code. In the MATLAB example function should start with a lower case letter not an upper case letter. This is due to the annoyance of word converting things to capitals when you don’t want it to. |
|
|
|
Jul 3 2005, 09:59 PM
Post
#5
|
|
|
Neurotical Squirrel ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 590 Joined: 4-November 04 From: Novi Sad, Vojvodina Member No.: 2,127 |
Long time no see
It's been a while since anyone posted anything here, so I'll just add this simple piece of code, hoping someone will add something new I recently remembered how to swap two variables without the need for the third variable CODE Dim v1 As Double, v2 As Double v1 = 17 v2 = 2 v1 = v1 + v2 v2 = v1 - v2 v1 = v1 - v2 This code of course won't swap variables that contain objects or are typed (struct) variables, but can be usefull if you have limited memory resources... Variables of course don't need to be double, they can be integer, long... Any size you need |
|
|
|
Jul 5 2005, 05:56 AM
Post
#6
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 378 Joined: 8-January 05 Member No.: 3,174 |
If you want one problem solved in many languages (besides "Hello world" obviously), you should check out 99 bottles of beer.
It's a website with (at the time I'm writing this) program code/scripts that print the lyrics for "99 bottles" (all the way down to the last one) in 729 variations. Those 729 variations do not mean there are 729 languages, but almost, and that includes some pretty weird languages, like whitespace. |
|
|
|
Jan 9 2006, 01:16 PM
Post
#7
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 130 Joined: 21-December 05 Member No.: 15,990 |
your function on PHP.
Is this workable? CODE <? function Fact(x){ if (x < 1){ Fact == 1;} else{ Fact == x * Fact(x - 1);} } ?> |
|
|
|
Jan 9 2006, 02:06 PM
Post
#8
|
|
|
.::UniCorN::. ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 973 Joined: 19-September 04 From: Dalian CHN Member No.: 1,192 |
well,i think DeveloperX gave the universal algorithm,which is a easy function recursive arithmetic,which remind me in C++:
CODE template<typename T> T function Fact(T n) {T r; if(n==0) r=1; else r=n*function Fact(n-1); return r;} |
|
|
|
Jul 28 2006, 07:39 PM
Post
#9
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 629 Joined: 26-February 05 Member No.: 3,995 |
Here are a couple more languages:
Python CODE def factorial(n): if n is 0 or n is 1: return 1 else: return n * factorial(n-1) Java CODE public static int factorial(int n) { if(n == 0 || n == 1) return 1; else return n * factorial(n-1); } PHP CODE function factorial($n) { if($n == 0 || $n == 1) return 1; else return $n * factorial($n-1); } Edit: I'd like to add that although the PHP and the Java look very similar, they are very different languages. You'll notice the "int" in the Java example. It's there because Java likes its variables to stay one type, while PHP doesn't really care. This Java code would not work: CODE String s = "11"; However, similar code in PHP would work:s++; //compile-time error CODE $s = "11";
$s++; //s is now 12 (an int, not a string) This post has been edited by beeseven: Jul 28 2006, 08:45 PM |