Jump to content
xisto Community
Sign in to follow this  
s243a

Short Programming Example (in Many Languages)

Recommended Posts

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)

fact 1=1fact 0=1fact x=fact(x-1)

Soln. (problem 1.) (MATLAB)
Function y=fact(x)if (y==1) |(y==0)    y=1else    y=x*fact(x-1)end

Share this post


Link to post
Share on other sites

As I can't think of any simple problem (I'm at work, and cracking my head with different problems :) ; I'm taking a break), I'll add a solution to factoriel problem, in Visual Basic. It generaly looks almost the same as the solution in matlab, with slight differences... It will be helpfull to beginners in VB, to see how to change the code in some other language, to VB...

Function Fact(ByVal x As Long) As LongIf x < 1 Then  Fact = 1Else  Fact = x * Fact(x - 1)End IfEnd Function

This is a great idea, and an opportunity for all of us to learn something new...

Share this post


Link to post
Share on other sites

I got my lisp interpreter to work and the correct code is:

(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 dont want it to.

Share this post


Link to post
Share on other sites

Long time no see :unsure:
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 :D

I recently remembered how to swap two variables without the need for the third variable

Dim v1 As Double, v2 As Doublev1 = 17v2 = 2v1 = v1 + v2v2 = v1 - v2v1 = 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 :D

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

well,i think DeveloperX gave the universal algorithm,which is a easy function recursive arithmetic,which remind me in C++:

template<typename T>T function Fact(T n){T r;if(n==0)r=1;elser=n*function Fact(n-1);return r;}

Share this post


Link to post
Share on other sites

Here are a couple more languages:

Python

def factorial(n):	 if n is 0 or n is 1:		  return 1	 else:		  return n * factorial(n-1)

Java
public static int factorial(int n) {	 if(n == 0 || n == 1)		  return 1;	 else		  return n * factorial(n-1);}

PHP
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:
String s = "11";s++;//compile-time error
However, similar code in PHP would work:
$s = "11";$s++;//s is now 12 (an int, not a string)

Edited by beeseven (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.