编程语言的代码示例

发布于 2024-09-12 09:50:06 字数 151 浏览 4 评论 0原文

当您设计新的编程语言或比较现有的编程语言时,您应该编写什么类型的代码示例?他们应该:

  • 强调语言的重要特征
  • 考虑重要/共同的特征
  • 允许通过示例进行学习。

另外,列出一些值得编写作为展示的简单算法?

When you are designing a new programming language, or comparing existing programming languages, what types of code examples should you write? They should:

  • emphasize the important features of the language(s)
  • take considerations into important/common features
  • allow learning by examples.

Also, list some simple algorithms that worth to be written as a showcase?

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

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

发布评论

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

评论(4

月下伊人醉 2024-09-19 09:50:06

代码示例应该:

  • 展示如何启动一个新的应用程序(例如 Hello World)
  • 展示如何执行常见模式(例如,函数式语言使用尾递归调用而不是循环)
  • 展示是什么使该语言独特/适合某些任务(例如 Ruby 中的元编程、Erlang 中的模式匹配)

The code examples should:

  • Show how to start a fresh app (ex. Hello World)
  • Show how to do common patterns (ex. Instead of loops, functional languages use tail-recursive calls)
  • Show what makes the language unique/right for certain tasks (ex. Meta-programming in Ruby, Pattern-matching in Erlang)
离去的眼神 2024-09-19 09:50:06

某种语言的最佳代码示例展示了为什么该语言在该特定代码段上比任何其他语言都更好。本质上与“hello world”相反。

The best code examples for a language demonstrate why that language is better than any other language for that particular piece of code. Essentially the opposite of "hello world".

你好,陌生人 2024-09-19 09:50:06

您应该编写真正的程序,这些程序由于新的语言功能而变得易于编写或扩展。如果您使用库,请考虑它们的复杂性。

当然,这不是我打算做的。编写您的编程语言时,您感兴趣的真实程序会变得更短、更好。只有白痴才会关心功能。你用编程语言编写程序,它不是装饰品。因此,您应该专注于程序开发任务,而忽略语言中对可用性无贡献的美学部分。

从可以编写程序的最简单的语言开始。以此为基础迭代改进语言,并解决早期版本和其他语言遇到的所有问题。

您正在通过编写一种新的编程语言来解决一个问题,对吗?强调如何用你的语言解决这个问题。

You should write real programs that become easy to write or extend because of your new language features. If you use libraries, account their complexity.

Of course this is nothing I propose to do. Write your programming language in a way that those real programs you're interested about become shorter and better. Only idiots care about features. You write programs with a programming language, it's not a decoration. Therefore you should concentrate on program development task and ignore aesthetic parts of your language that do not contribute on the usability.

Start with the simplest language you can write a program on. Improve the language iteratively from that and work with all issues you had with earlier versions and other languages.

There's a problem you're solving by writing a new programming language, right? Emphasize how that problem gets solved with your language.

很糊涂小朋友 2024-09-19 09:50:06

我在较新的动态语言中更常见的一个例子是简单的静态 Web 服务器。几年前,我第一次看到 Tcl(不是一种新语言)的小型 Web 服务器示例。现在看来大多数新语言都有一个用不到 50 行代码编写的 Web 服务器。

Google 的 Go 语言实际上有一个微型网络服务器,作为其官方文档中的示例代码之一。尽管 Go 通过使用库做了一些欺骗。但这很好地展示了其网络库的优秀程度。 Node.js 的官方文档中还包含一个 Web 服务器示例。

如果您的语言支持用 50(100?)行代码编写一个简单的 Web 服务器,那么您应该使用它作为示例。 Web 服务器是一个很好的例子,因为它向您展示了该语言如何处理网络、文件 I/O 和字符串操作。让我们面对现实吧,除了 3D 游戏和物理模拟之外,现在大多数代码更多地处理网络、文件 I/O 和字符串,而不是数字。

One example I see more often in newer dynamic languages is a simple static web server. I first saw an example of a tiny web server in Tcl (not a new language) a few years ago. Now it seems most new languages have a web server written in under 50 lines of code.

Google's Go language actually has a tiny web server as one of the example code in its official documents. Although Go cheats a bit by using a library. But it's a good showcase of how good its networking library is. Node.js also include a web server example in its official docs.

If your language support writing a simple web server in under 50 (100?) lines of code then you should use it as an example. A web server is a good example because it shows you how the language handles networking, file I/O and string manipulation. And lets face it, apart from 3D games and physics simulations most code these days deal more with networking, file I/O and strings than numbers.

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