对于初学者来说,Haskell 还是标准机器学习?

发布于 2024-07-18 04:09:14 字数 1431 浏览 8 评论 0原文

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

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

发布评论

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

评论(8

绝影如岚 2024-07-25 04:09:14

尽管我很喜欢 Haskell,但以下是我更喜欢将 SML 用于离散数学和数据结构课程(以及大多数其他初学者课程)的原因:

  • Haskell 程序的时间和空间成本可能很难预测,即使对于专家来说。 SML 提供了更有限的方法来破坏机器。

  • 交互式解释器中函数定义的语法与文件中使用的语法相同,因此您可以剪切和粘贴。

  • 尽管 SML 中的运算符重载完全是假的,但它也很简单。 如果不进入类型类,就很难在 Haskell 中教授整个课程。

  • 学生可以使用print进行调试。 (尽管,正如评论者指出的那样,使用 Debug.Trace.trace 在 Haskell 中可以获得几乎相同的效果。)

  • 无限的数据结构令人震惊。 对于初学者来说,最好让他们定义一个包含引用单元和 thunk 的流类型,这样他们就知道它是如何工作的:

    数据类型 'a thunk_contents = 单元未评估 ->   'A 
                                 |   'a 的值 
      输入 'a thunk = 'a thunk_contents ref 
      val 延迟 : (unit -> 'a) ->   '一声重响 
      val force : '一声重击 ->   'A 
      

    现在它不再是魔法了,你可以从这里转到流(无限列表)。

  • 布局不像 Python 中那么简单,并且可能会令人困惑。

Haskell 有两个优势:

  • 在 Haskell 核心中,您可以在函数定义之前编写函数的类型签名。 这对于学生和其他初学者来说非常有帮助。 在 SML 中没有一个好的方法来处理类型签名。

  • Haskell 有更好的具体语法。 Haskell 语法是对 ML 语法的重大改进。 我写了一篇关于何时在机器学习程序; 这有点帮助。

最后,有一把双向利剑:

  • Haskell 代码默认是纯的,因此您的学生不太可能意外地遇到不纯的构造(IO monad、state monad)。 但出于同样的原因,它们无法打印,如果你想做 I/O,那么至少你必须解释 do 表示法,而 return 会令人困惑。

关于相关主题,这里有一些针对您的课程准备的建议:不要忽视纯函数数据克里斯·冈崎的结构。 即使您不让您的学生使用它,您也肯定会想要一份副本。

Much as I love Haskell, here are the reasons I would prefer SML for a class in discrete math and data structures (and most other beginners' classes):

  • Time and space costs of Haskell programs can be very hard to predict, even for experts. SML offers much more limited ways to blow the machine.

  • Syntax for function defintion in an interactive interpreter is identical to syntax used in a file, so you can cut and paste.

  • Although operator overloading in SML is totally bogus, it is also simple. It's going to be hard to teach a whole class in Haskell without having to get into type classes.

  • Student can debug using print. (Although, as a commenter points out, it is possible to get almost the same effect in Haskell using Debug.Trace.trace.)

  • Infinite data structures blow people's minds. For beginners, you're better off having them define a stream type complete with ref cells and thunks, so they know how it works:

    datatype 'a thunk_contents = UNEVALUATED of unit -> 'a
                               | VALUE of 'a
    type 'a thunk = 'a thunk_contents ref
    val delay : (unit -> 'a) -> 'a thunk
    val force : 'a thunk -> 'a
    

    Now it's not magic any more, and you can go from here to streams (infinite lists).

  • Layout is not as simple as in Python and can be confusing.

There are two places Haskell has an edge:

  • In core Haskell you can write a function's type signature just before its definition. This is hugely helpful for students and other beginners. There just isn't a nice way to deal with type signatures in SML.

  • Haskell has better concrete syntax. The Haskell syntax is a major improvement over ML syntax. I have written a short note about when to use parentheses in an ML program; this helps a little.

Finally, there is a sword that cuts both ways:

  • Haskell code is pure by default, so your students are unlikely to stumble over impure constructs (IO monad, state monad) by accident. But by the same token, they can't print, and if you want to do I/O then at minumum you have to explain do notation, and return is confusing.

On a related topic, here is some advice for your course preparation: don't overlook Purely Functional Data Structures by Chris Okasaki. Even if you don't have your students use it, you will definitely want to have a copy.

深陷 2024-07-25 04:09:14

我们向大学一年级学生教授 Haskell。 我对此的感受有点复杂。 一方面,向一年级学生教授 Haskell 意味着他们不必忘记命令式风格。 Haskell 还可以生成非常简洁的代码,以前学过 Java 的人都可以欣赏。

我注意到学生经常遇到的一些问题:

  • 模式匹配一​​开始可能有点困难。 学生们最初在理解价值构建和模式匹配之间的关系时遇到了一些问题。 他们在区分抽象之间也存在一些问题。 我们的练习包括编写简化算术表达式的函数,一些学生很难看出抽象表示(例如,Const 1)和元语言表示(1)之间的区别。

    此外,如果您的学生应该自己编写列表处理函数,请小心指出模式之间的差异

    <前><代码>[]
    [X]
    (x:xs)
    [x:xs]

    根据您想教他们多少函数式编程,您可以只给他们一些库函数,让他们尝试一下。

  • 我们没有教学生有关匿名函数的知识,我们只是告诉他们有关 where 子句的知识。 对于某些任务来说,这有点冗长,但在其他方面效果很好。 我们也没有告诉他们有关部分申请的信息; 这在 Haskell 中可能很容易解释(由于其书写类型的形式),因此可能值得向他们展示。

  • 很快发现了列表推导式,并且比 filtermapzipWith 等高阶函数更喜欢它们。

  • 我认为我们错过了一些教导他们如何让他们通过类型来引导他们的想法的机会。 不过,我不太确定这对初学者是否有帮助。

  • 错误消息通常对初学者没有太大帮助,他们可能偶尔需要一些帮助。 我自己没有尝试过,但是有一个专门针对新手的 Haskell 编译器,主要是通过更好的错误消息: Helium

  • 对于小程序来说,可能的空间泄漏之类的事情不是问题。

总的来说,Haskell 是一种很好的教学语言,但也存在一些缺陷。 鉴于学生对列表推导式感觉比高阶函数更舒服,这可能就是您需要的论点。 我不知道你的课程有多长,也不知道你想教他们多少编程,但一定要计划一些时间来教他们基本概念——他们会需要它。

We teach Haskell to first years at our university. My feelings about this are a bit mixed. On the one hand teaching Haskell to first years means they don't have to unlearn the imperative style. Haskell can also produce very concise code which people who had some Java before can appreciate.

Some problems I've noticed students often have:

  • Pattern matching can be a bit difficult, at first. Students initially had some problems seeing how value construction and pattern matching are related. They also had some problems distinguishing between abstractions. Our exercises included writing functions that simplify arithmetic expression and some students had difficulty seeing the difference between the abstract representation (e.g., Const 1) and the meta-language representation (1).

    Furthermore, if your students are supposed to write list processing functions themselves, be careful pointing out the difference between the patterns

    []
    [x]
    (x:xs)
    [x:xs]
    

    Depending on how much functional programming you want to teach them on the way, you may just give them a few library functions and let them play around with that.

  • We didn't teach our students about anonymous functions, we simply told them about where clauses. For some tasks this was a bit verbose, but worked well otherwise. We also didn't tell them about partial applications; this is probably quite easy to explain in Haskell (due to its form of writing types) so it might be worth showing to them.

  • They quickly discovered list comprehensions and preferred them over higher-order functions like filter, map, zipWith.

  • I think we missed out a bit on teaching them how to let them guide their thoughts by the types. I'm not quite sure, though, whether this is helpful to beginners or not.

  • Error messages are usually not very helpful to beginners, they might occasionally need some help with these. I haven't tried it myself, but there's a Haskell compiler specifically targeted at newcomers, mainly by means of better error messages: Helium

  • For the small programs, things like possible space leaks weren't an issue.

Overall, Haskell is a good teaching language, but there are a few pitfalls. Given that students feel a lot more comfortable with list comprehensions than higher-order functions, this might be the argument you need. I don't know how long your course is or how much programming you want to teach them, but do plan some time for teaching them basic concepts--they will need it.

绾颜 2024-07-25 04:09:14

顺便提一句,

# SML 具有真正的交互性
解释器,其中的函数可以是
既定义又使用。 在哈斯克尔中,
函数必须定义在
单独的文件并之前编译
在交互式 shell 中使用。

不准确。 使用 GHCi:

Prelude> let f x = x ^ 2
Prelude> f 7
49
Prelude> f 2
4

haskell.org edu 上也有很好的 Haskell 教育资源。 页面,包含不同老师的经验。 http://haskell.org/haskellwiki/Haskell_in_education

最后,您将能够教他们多核并行性只是为了好玩,如果你使用 Haskell :-)

BTW,

# SML has a truly interactive
interpreter in which functions can be
both defined and used. In Haskell,
functions must be defined in a
separate file and compiled before
being used in the interactive shell.

Is inaccurate. Use GHCi:

Prelude> let f x = x ^ 2
Prelude> f 7
49
Prelude> f 2
4

There are also good resources for Haskell in education on the haskell.org edu. page, with experiences from different teachers. http://haskell.org/haskellwiki/Haskell_in_education

Finally, you'll be able to teach them multicore parallelism just for fun, if you use Haskell :-)

趁年轻赶紧闹 2024-07-25 04:09:14

许多大学将 Haskell 作为第一门函数式语言甚至第一门编程语言来教授,所以我认为这不会成为问题。

在对这样一门课程进行过一些教学之后,我不同意您所发现的可能存在的困惑有那么可能。 早期混乱最可能的来源是由错误布局引起的解析错误,以及错误使用数字文字时有关类型类的神秘消息。

我也不同意任何关于不建议初学者使用 FP 的建议。 这当然是一种大爆炸方法,而具有突变的严格语言则不然,但我认为这是一种非常有效的方法。

Many universities teach Haskell as a first functional language or even a first programming language, so I don't think this will be a problem.

Having done some of the teaching on one such course, I don't agree that the possible confusions you identify are that likely. The most likely sources of early confusion are parsing errors caused by bad layout, and mysterious messages about type classes when numeric literals are used incorrectly.

I'd also disagree with any suggestion that Haskell is not recommended for beginners starting out with FP. It's certainly the big bang approach in ways that strict languages with mutation aren't, but I think that's a very valid approach.

塔塔猫 2024-07-25 04:09:14
  • SML 拥有真正的交互式解释器,可以在其中定义和使用函数。 在 Haskell 中,函数必须在单独的文件中定义并在交互式 shell 中使用之前进行编译。

虽然 Hugs 可能有这个限制,但 GHCi 没有:

$ ghci
GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> let hello name = "Hello, " ++ name
Prelude> hello "Barry"
"Hello, Barry"

我更喜欢 GHC(i) 而不是 Hugs 的原因有很多,这只是其中之一。

  • SML 以易于理解的语法明确确认函数参数和返回类型。 例如: val foo = fn : int * int -> 国际。 Haskell 的隐式柯里化语法有点迟钝,但并非完全陌生。 例如: foo::Int -> 整数-> 国际。

SML 也具有所谓的“隐式柯里化”语法。

$ sml
Standard ML of New Jersey v110.69 [built: Fri Mar 13 16:02:47 2009]
- fun add x y = x + y;
val add = fn : int -> int -> int

本质上,SML 和 Haskell 大致是等价的。 我倾向于 Haskell,因为我喜欢 Haskell 中的列表推导式和无限列表。 但我担心 Haskell 紧凑语法中的大量符号可能会给学生带来问题。 根据我阅读有关 SO 的其他文章所收集的信息,不建议初学者从 FP 开始使用 Haskell。 但我们不会构建成熟的应用程序,只是尝试简单的算法。

与 SML 相比,我更喜欢使用 Haskell,但我仍然会先教 SML。

  • 支持诺米诺洛的想法,列表推导式似乎会减慢学生学习某些高阶函数的速度。
  • 如果您想要惰性和无限列表,那么显式实现它会很有帮助。
  • 因为 SML 是热切评估的,所以执行模型更容易理解,并且“通过 printf 进行调试”比 Haskell 中的效果要好得多。
  • SML 的类型系统也更简单。 虽然你的类可能无论如何都不会使用它们,但 Haskell 的类型类仍然是一个需要克服的额外障碍——让他们理解 'a''a 的区别SML已经足够坚韧了。
  • SML has a truly interactive interpreter in which functions can be both defined and used. In Haskell, functions must be defined in a separate file and compiled before being used in the interactive shell.

While Hugs may have that limitation, GHCi does not:

$ ghci
GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> let hello name = "Hello, " ++ name
Prelude> hello "Barry"
"Hello, Barry"

There's many reasons I prefer GHC(i) over Hugs, this is just one of them.

  • SML gives explicit confirmation of the function argument and return types in a syntax that's easy to understand. For example: val foo = fn : int * int -> int. Haskell's implicit curry syntax is a bit more obtuse, but not totally alien. For example: foo :: Int -> Int -> Int.

SML has what you call "implicit curry" syntax as well.

$ sml
Standard ML of New Jersey v110.69 [built: Fri Mar 13 16:02:47 2009]
- fun add x y = x + y;
val add = fn : int -> int -> int

Essentially, SML and Haskell are roughly equivalent. I lean toward Haskell because I'm loving the list comprehensions and infinite lists in Haskell. But I'm worried that the extensive number of symbols in Haskell's compact syntax might cause students problems. From what I've gathered reading other posts on SO, Haskell is not recommended for beginners starting out with FP. But we're not going to be building full-fledged applications, just trying out simple algorithms.

I like using Haskell much more than SML, but I would still teach SML first.

  • Seconding nominolo's thoughts, list comprehensions do seem to slow students from getting to some higher-order functions.
  • If you want laziness and infinite lists, it's instructive to implement it explicitly.
  • Because SML is eagerly evaluated, the execution model is far easier to comprehend, and "debugging via printf" works a lot better than in Haskell.
  • SML's type system is also simpler. While your class likely wouldn't use them anyways, Haskell's typeclasses are still an extra bump to get over -- getting them to understand the 'a versus ''a distinction in SML is tough enough.
油饼 2024-07-25 04:09:14

大多数答案都是技术性的,但我认为您至少应该考虑一个不是技术性的:Haskell(作为 OCaml)目前有一个更大的社区,在更广泛的上下文中使用它。 Hackage 上还有一个大型数据库,其中包含为盈利和娱乐而编写的库和应用程序。 这可能是让你的一些学生在课程结束后继续使用该语言的一个重要因素,并且可能会在以后尝试其他函数式语言(例如标准机器学习)。

Most answers were technical, but I think you should consider at least one that is not: Haskell (as OCaml), at this time, has a bigger community using it in a wider range of contexts. There's also a big database of libraries and applications written for profit and fun at Hackage. That may be an important factor in keeping some of your students using the language after your course is finished, and maybe trying other functional languages (like Standard ML) later.

伴我老 2024-07-25 04:09:14

我很惊讶您没有考虑 OCaml 和 F#,因为它们解决了您的许多担忧。 体面且有益的发展环境肯定是学习者的首要任务吗? 在这方面,SML 远远落后,而 F# 则远远领先于所有其他 FPL。

此外,OCaml 和 F# 都具有列表推导式。

I am amazed you are not considering OCaml and F# given that they address so many of your concerns. Surely decent and helpful development environments are a high priority for learners? SML is way behind and F# is way ahead of all other FPLs in that respect.

Also, both OCaml and F# have list comprehensions.

晨曦÷微暖 2024-07-25 04:09:14

哈斯克尔。 由于我从使用 Haskell 中学到的东西,我在计算机科学的算法/理论课上取得了领先。 这是一门非常全面的语言,只要使用它就能教会你大量的计算机科学知识。

然而,SML 更容易学习。 Haskell 具有惰性求值和控制结构等功能,使其更加强大,但代价是学习曲线陡峭。 SML没有这样的曲线。

也就是说,大多数 Haskell 都放弃了从 Ruby、ObjC 或 Python 等不太科学/数学的语言中学到的东西。

Haskell. I'm ahead in my algos/theory class in CS because of the stuff I learned from using Haskell. It's such a comprehensive language, and it will teach you a ton of CS, just by using it.

However, SML is much easier to learn. Haskell has features such as lazy evaluation and control structures that make it much more powerful, but with the cost of a steep(ish) learning curve. SML has no such curve.

That said, most of Haskell was unlearning stuff from less scientific/mathematic languages such as Ruby, ObjC, or Python.

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