寻找用不同语言编写的示例程序

发布于 2024-07-18 07:29:35 字数 1542 浏览 12 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

維他命╮ 2024-07-25 07:29:35

我认为这不会教给你太多。 在不同编程范式之间的差异真正显现出来之前,程序必须具有一定的大小。 如果同一个程序用不同的语言,人们不太可能编写相同的副本,除非该程序很简单。

大多数现实生活中的示例也会受到大量额外噪音的污染,这些事情可以在一种语言的标准库中完成,但需要另一种语言的第三方库。 编写它的程序员可能比另一种语言更熟悉一种语言,因此他在某些语言中的实现并不代表它“应该”如何完成。

您更有可能以通常的方式了解这些范例之间的区别。 通过学习每种含义以及如何使用它。

I don't think this is likely to teach you much. The program has to have a certain size before the differences between different programming paradigms really show. And people aren't likely to write identical copies if the same program in different languages unless the program is trivial.

Most real-life examples would also be polluted with a lot of extra noise, things that can be done within the standard library of one language, but requires third-party libraries in another. And the programmer who wrote it may be more familiar with one language than another, so his implementation in some languages isn't representative of how it "should" be done.

You're more likely to learn the difference between these paradigms the usual way. By learning what each means, and how to use it.

层林尽染 2024-07-25 07:29:35

我推荐99瓶啤酒网站

I recommend the 99 bottles of beer website

云巢 2024-07-25 07:29:35

您可以随时查看Project Euler。 人们用多种不同的语言解决相同的问题。 大多数人都会发布他们的解决方案,您在解决问题后也可以访问这些解决方案。

You can always look at Project Euler. People solves the same problems in a many different languages. Most people will post their solutions that you can access after you solve the problem also.

一个人的旅程 2024-07-25 07:29:35

看看计算机语言基准测试游戏。 它几乎以您能想象到的所有语言实现了各种程序。

Take a look at The Computer Language Benchmarks Game. It's got implementations of various programs in just about every language you could imagine.

泪是无色的血 2024-07-25 07:29:35

对于您的目的来说,这可能有点简单,但是 Hello World Collection 始终是浏览起来很有趣。

This might be a bit simple for your purposes but the Hello World Collection is always fun to look through.

忘你却要生生世世 2024-07-25 07:29:35

Rosetta Code 拥有大量数据,但与过程/面向对象区别相关的数据很少。 您还应该查看他们的相关网站的集合。

Rosetta Code has a wealth of data but very little of it is related to the procedural/object-oriented distinction. You should also see their collection of related sites.

红墙和绿瓦 2024-07-25 07:29:35

多种语言的 Black Scholes 有大量 Black-Scholes 公式的实现。
该公式在 Objective-C/iPhone、F#、Autoit、Fortress、Lua、APL、SAS、Mathcad、J、MEL、Postscript、VB.NET、Clean、Ruby、Lisp、Prolog、PL/SQL、LyME、ColdFusion 中实现、K、C#、HP48、Transact SQL、O'Caml、Rebol、Real Basic、Icon、Squeak、Haskell、JAVA、JavaScript、VBA、C++、Perl、Maple、Mathematica、Matlab、S-Plus、IDL、Pascal、Python 、Fortran、Scheme、PHP、GNU、gnuplot。

Black Scholes in multiple languages has plenty of implementations of the Black-Scholes formula.
The formula is implemented in Objective-C/iPhone, F#, Autoit, Fortress, Lua, APL, SAS, Mathcad, J, MEL, Postscript, VB.NET, Clean, Ruby, Lisp, Prolog, PL/SQL, LyME, ColdFusion, K, C#, HP48, Transact SQL, O'Caml, Rebol, Real Basic, Icon, Squeak, Haskell, JAVA , JavaScript, VBA, C++, Perl, Maple, Mathematica, Matlab, S-Plus, IDL, Pascal, Python, Fortran, Scheme, PHP, GNU, gnuplot.

⒈起吃苦の倖褔 2024-07-25 07:29:35

前几天有人在 Reddit 上发布了 Evil Walrus / ReFactory:

http://www.refactory.org/

Somebody posted Evil Walrus / ReFactory on Reddit the other day:

http://www.refactory.org/

娇妻 2024-07-25 07:29:35

这里有两个实现 n-body 的程序

Java实现

C 实现

您发现它们之间有什么区别?

Here are two programs that implement n-body

Java implementation

C implementation

What differences do you find between them?

挽心 2024-07-25 07:29:35

考虑蛇和梯子游戏的实现

在程序设计中,我们可能会编写一个函数,就像

function move(int n) {
    pos += n;
    switch(pos) {
        case 6: pos = 10; break;
        case 12: pos = 4; break;
        case 15: pos = 32; break;
        case 16: pos = 8; break;

        // ...

    }
 }

在对象设计语言中一样,我们将创建一个 Square 实例的链接列表,以及一些 LadderSnake 分支到其他方块的实例。

class Square
  def initialize(next)
    @tokens = []
    @next = next
  end
  def next(n)
    n == 0 ? self : next.next(n-1)
  end
  def move(token,n)
    tokens.remove(token)
    target = self.next(n)
    target.tokens << token
  end
end

class SnakeOrLadder < Square
  def initialize(next,branch)
    super(next)
    @branch = branch
  end
  def next(n)
    # goes to branch when landing on this square!
    n == 0 ? @branch : next.next(n-1)
  end
end

正如您所看到的,我们在对象中以及它们的组合方式(而不是在 switch case 语句中)实现游戏规则。 这样做的优点是,

  • 在开发时添加新的游戏规则很简单,您只需编写 Square 的新子类,
  • 在运行时更改游戏的布局很简单(对于游戏来说可能听起来很奇怪,但对于您的游戏来说)普通的商业应用程序,这就是您想要的)

这种灵活性使得 OO 如此强大。

Consider the implementation of a snakes and ladders games

In a procedural design we might write a function like

function move(int n) {
    pos += n;
    switch(pos) {
        case 6: pos = 10; break;
        case 12: pos = 4; break;
        case 15: pos = 32; break;
        case 16: pos = 8; break;

        // ...

    }
 }

in an object design language we would create a linked list of Square instances, with some Ladder and Snake instances that branch to other squares.

class Square
  def initialize(next)
    @tokens = []
    @next = next
  end
  def next(n)
    n == 0 ? self : next.next(n-1)
  end
  def move(token,n)
    tokens.remove(token)
    target = self.next(n)
    target.tokens << token
  end
end

class SnakeOrLadder < Square
  def initialize(next,branch)
    super(next)
    @branch = branch
  end
  def next(n)
    # goes to branch when landing on this square!
    n == 0 ? @branch : next.next(n-1)
  end
end

as you can see, we implement the game rules in the objects as well as the way they are composed (rather than in a switch case statement). This has the advantage that

  • it is simple to add new game rules at development time, you'll just write a new subclass of Square
  • it is simple to change the layout of the game at runtime (might sound strange for a game, but for your average business app, this is what you want)

this flexibility is what makes OO so powerful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文