Jul 26, 2008

Short Programming Example (in Many Languages)

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Others

free web hosting

Short Programming Example (in Many Languages)

s243a
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

Reply

Galahad
As I can't think of any simple problem (I'm at work, and cracking my head with different problems laugh.gif ; 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...

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...

Reply

s243a
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

 

 

 


Reply

s243a
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.

Reply

Galahad
Long time no see laugh.gif
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 smile.gif

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 smile.gif

Reply

bjrn
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.


Reply

DeveloperX
your function on PHP.
Is this workable?

CODE

<?
function Fact(x){
if (x < 1){
  Fact == 1;}
else{
Fact == x * Fact(x - 1);}
}
?>

Reply

hulunes
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;}

Reply

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

Reply

Lobosque
That is the ruby function:
CODE
def factorial(i)
     if i == 1 || i == 0
          return 1
     else
          return i * factorial(i-1)
     end
end

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. factorial code in matlab - 28.08 hr back. (1)
  2. factorial program matlab - 28.79 hr back. (1)
  3. matlab factorial code - 29.65 hr back. (2)
  4. return the factorial of the integer in matlab - 61.52 hr back. (2)
  5. "pascal programming" array swap @ - 100.08 hr back. (1)
  6. factorial matlab - 102.87 hr back. (1)
  7. prolog solution swap/2 - 172.67 hr back. (1)
Similar Topics

Keywords : programming languages

  1. A Prelude To Programming - What to learn? How to learn? (9)
  2. Python - another programming language (8)
    There are so many languages used in software and web development. Python is one of them. Basically
    Python is object-oriented , high-level programming language with dynamic semantics. Python was
    developed in the 90’s. Python is simple and easy to learn. Therefore it reduces cost of program
    maintenance. It is very attractive for rapid application development because of its high-level
    built-in data structures, combined with dynamic typing and dynamic binding. Python is also use as a
    scripting or glue language to connect existing components. Python supports modules and ...
  3. Programming Batch Job - (6)
    I want to know how to make a batch file that will run a program at a certain time of day, like a
    scheduled thing, but i want it in a batch.. lol.. Easy enough? If you give me permission i can edit
    it to the program i want Lol.. just put it in code tags! Thanks! Topic title must be
    specific. Modified. ...
  4. Python - Programming Language (4)
    Does anybody else know any python? I've started learning it, because I heard somewhere that it
    is quite easy to learn and I want to learn a programming language quickly /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />. I haven't learned how
    to do any complex stuff with it yet, but its still fun learning about it /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />. If you want to find out
    more info about Python, then you can go to their website. I'm not sure if i'm allowed to
    post links...
  5. Things You Should Know - things you should know about programming (0)
    This should help you know what programming languages are. C# is a language designed by Microsoft
    for the .Net framework. Its syntax is similar to C and C++, with an emphasis on being programmer
    friendly. It has memory management where C and C++ require the programmer to do this manually, it
    has array bounds checking where C and C++ don't, among many other differences. Automatic memory
    management and some other programmer-friendly features of C# sacrifice some performance, but these
    features can also make development faster and easier. This can be very attractive to ...
  6. Mac Programming Languages? - What are some good ones? (5)
    What are some good Mac Programming Languages? /huh.gif" style="vertical-align:middle" emoid=":huh:"
    border="0" alt="huh.gif" /> I have a friend from school who is like obssed with Mac and Linux (And
    thinks windows sucks when it doesn't /mad.gif" style="vertical-align:middle" emoid=":angry:"
    border="0" alt="mad.gif" /> ), and it would be cool if I could learn some Mac Programming languages
    aside from what I already know about Programming, so I could make some cool programs for mac too,
    show off, and beat him at his own game. /laugh.gif" style="vertical-align:middle...
  7. Liberty Basic And Qbasic - Also Qbasic's Sister Languages -- FreeBasic or GW-BASIC (3)
    Have you ever heard of Liberty BASIC or Qbasic -- Or Qbasic's Sister Languages, FreeBasic or
    GW-BASIC? They are pretty old, but I still use it though. /tongue.gif"
    style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" /> These are good languages to
    start if you have never programmed before, as it has the base and will help you fully understand the
    concept of most other languages. /wink.gif" style="vertical-align:middle" emoid=";)" border="0"
    alt="wink.gif" /> Liberty BASIC is making its come back. Even though its as much as $119.95 if
    you get a...
  8. Future Of Programming... - Real Programmers Respond Please (6)
    I was watching Star Trek the other day, I think it was Voyager. Anyways I was wondering.. They refer
    to this 'trinary' code and other things like Tera Quads of information. I researched this a
    little bit and all I could find were entries about it relating to Star Trek and nothing else really.
    So I was wondering, does anyone really know or have a really good explanation of what is on the
    horizen for programming? Because I'm sure that since we created Binary and so many other ways to
    make computers work, isn't there something in the future to be made that w...
  9. Sql Programming By Heaven_master_ash Own - (1)
    Well This Is A Very very Easy Topic Every One Know About This thing If U Need Help To Understand
    Better Refer My Knowledge Gatherance Thanx.. CODE The SQL SELECT statement queries data from
    tables in the database. The statement begins with the SELECT keyword. The basic SELECT statement has
    3 clauses:     * SELECT     * FROM     * WHERE The SELECT clause specifies the table columns
    that are retrieved. The FROM clause specifies the tables accessed. The WHERE clause specifies which
    table rows are used. The WHERE clause is optional; if missing, all table rows ar...
  10. Programming Artificial Intelligence - oh yeah... (4)
    I've been interested in the idea of working with AI later on in my career when I actually get
    the resources to do so (After college is finished). Does anyone here already know how to program
    basic AI that might be able to help get me started?...
  11. Pascal Programming - (5)
    I don't se any sub forum for pascal so I will post this here, I have some problems, how to set
    this: i need to create program for seraching and deleting some files, if you have some examples or
    source code that will be great, and how to create some skin, or I don't know to create one
    normal program not the one like the dos thanks...
  12. What Programming Languages Do You Use? - (26)
    So? What do you use? I use Turing ...
  13. Shakespeare Programming Language - (2)
    http://shakespearelang.sourceforge.net/report/shakespeare/ This is a weird language whose syntax
    is kind of like a play. It may not seem like it can do anything, but it's actually more powerful
    that it looks. The programs are much longer than those in any other language, but SPL is more fun.
    Unfortunately, there's no compiler-it has to be translated to C, then compiled from there. There
    are some example programs at the bottom....
  14. Programming From Ground Up - Best book for learning programming (1)
    This is one of the best book for learning programming with assembly language. This is a free book
    with GPL licence so this is not illegle This tutorial section states that QUOTE Contribute
    only full How-To's and Tutorials here. I feel that you did not contribute. Contribute is
    defined here as writing your own with your own words--we are looking for original tutorials. But
    thank you for the supplement. Moving from Tutorials to Computers > Programming Languages >
    Others Leaving a link behind ...
  15. What Programming Language Is Most Popular In Industry - What Programming language is most popular in industry (6)
    Hi All, I don't see any thread discuss about the pros and cons of different languages to help
    beginners, but i'm asking for a slightly different reason. I have been programming for about 1
    year now, and I know only Visual Basic. I am planning to learn some other programming langauges.
    I've learned that once you've learned one language, pretty much all you have to do is learn
    a new syntax to learn a new language. So, now I'll get to the point. I think that I should begin
    to specialize in a language. I don't know which language I should learn and ...
  16. Tried Logical Programming? - (2)
    Has anyone every looked at logical programming yet? I am interested in all sorts of programming
    paradigms. Logical programming looks kind of neat. Apparently it can be used to generate you own
    language parson. I found a fairly gentle introduction to prolog at: It gives some simple code to
    some neat problems http://www.csupomona.edu/~jrfisher/www/pro...l/contents.html You can also
    download a free compiler at: http://www.freeprogrammingresources.com/lispcompiler.html ...
  17. Newb @ Programming Seeking Suggestions - (3)
    Hey all, I'm new to programming and I have a hard time deciding on a cross-platform language
    so I can write for Linux, Macintosh, and Windows. I want to make some basic programs, and if
    possible (as in language support), games. I know that Java is one of them, so is Perl. Please
    reply. Thanks, xboxrulz...
  18. Programming Paradigms - (0)
    This discussion is created to discuss current programming paradigms and propose new programming
    paradigms. First Some thoughts: Functional programming is often used in AI languages because it
    allows a top down design approaches. The program can begin by describing the high level
    functionality and later flesh out the low level details. This is in contrast to procedural
    programming in which the problem is first broken down into smaller problems. The smaller problems
    are solved and then put together to solve the larger problem. One functional programming is called
    lisp....
  19. Palm Os Programming? - (2)
    Can anybody point me to some nice tutorials for Palm OS programming? I've got both a windows and
    a linux machine, but windows is prefered. (my linux box is currently non-functioning)...
  20. Ftp With Lots Of Programming Books - Don't wait to see (5)
    Here it is: CODE http://ftp.cdut.edu.cn/pub3/uncate_doc/ Carpe diem....
  21. TCL - Web programming with TCL (3)
    I was just wandering whether there are some old timer there using TCL to do CGI programming. It
    would be nice to hear some of the experiences you have had with it. I had to use it for something
    really specific, but now it turns out to be quite fun using it for CGI related situations....
  22. Programming E-books - and others too (0)
    Here it is: CODE http://n3t.net/TheVault/Library/ Username: ap Password: appz
    Carpe diem....



Looking for short, programming, languages

Searching Video's for short, programming, languages
advertisement



Short Programming Example (in Many Languages)



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE