标准机器学习入门

发布于 2024-08-11 01:23:49 字数 535 浏览 6 评论 0原文

我正在寻找某种“面向初学者的 ML”指南 - 到目前为止,谷歌已经引导我找到了一些晦涩的邮件列表或令人难以理解的文本。

问题是,到目前为止,我的函数式编程经验为零,并且事实证明,理解这些概念比预期要困难得多。例如,我现在想做的任务(是的,大学:))是在两个列表(a,b)和(c,d)之间进行映射,以便结果是(a,d)的列表) 来自 b = c 的对。

我通常会做的是:

result = []
for each x in list1 do
  for each y in list2 do
    if x.b == y.c result.add (x.a, y.d)
  end
end

但是,我什至无法开始在 SML 中实现类似的东西。例如,我知道我可以迭代带有嵌套 map f list 的列表。但是,运行时 (SML/NJ) 仅接受仅将列表元素作为参数的函数。但是,如果我一次只能操作两个列表之一,我如何知道要返回哪些元素呢?

我迫切需要一种可以解决此类问题的资源,最好是带有友好的大字母和大量代码示例:/

I'm looking for some kind of "ML for beginners" guide - google has led me to some obscure mailing lists or way-over-my-head texts so far.

The problem is, I have zero functional programming experience so far, and wrapping my head around the concepts has turned out far more difficult than expected. For example, the task I'm trying to do now (yes, university :) ) is do a mapping between two lists (a,b) and (c,d), so that the result is a list of (a,d) from pairs where b = c.

What I would ordinarily do is:

result = []
for each x in list1 do
  for each y in list2 do
    if x.b == y.c result.add (x.a, y.d)
  end
end

But, I can't even get started with implementing something like that in SML. For example, I know I could iterate over lists with nested map f list's. However, the runtime (SML/NJ) will only accept this for functions that take only list element as parameter. But how do I know what elements to return if I can only operate with one of the two lists at time?

I am in desperate need of a resource that would go over this kind of thing, preferably with big friendly letters and lots of code examples :/

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

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

发布评论

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

评论(6

千里故人稀 2024-08-18 01:23:49

标准机器学习编程,作者:CMU 的 Robert Harper 教授

Programming in Standard ML by Prof. Robert Harper of CMU

羁客 2024-08-18 01:23:49

我收藏了一些 SML 资源:

标准 ML '97 中的编程:在线教程
基本标准机器学习 (Amazon Kindle)
ML 编程元素 (amazon.com)

I have some SML resources bookmarked:

Programming in Standard ML '97: An On-line Tutorial
Elementary Standard ML (Amazon Kindle)
Elements of ML Programming (amazon.com)

冰魂雪魄 2024-08-18 01:23:49

对于您提到的练习,请考虑一个返回新列表而不是修改现有列表的函数 add,并考虑如何用您最喜欢的高级语言递归地实现该练习,这将是第一步。

您不需要使用 map 或任何其他现有的高阶 ML 函数!这些只是有经验的程序员的捷径。仅使用模式匹配和递归。但是,如果您寻找常用的递归和模式匹配的示例,则可以查看诸如 map 之类的函数的实现。

For the exercise that you mention, think of a function add that returns a new list instead of modifying the existing one, and consider how you would implement the exercise recursively in your favorite high-level language, that will be a first step.

You don't need to use map or any other existing higher-order ML function! These are just shortcuts for experienced programmers. Use pattern-matching and recursion only. But you can look at the implementation of functions such as map if you seek examples of well-used recursion and pattern-matching.

朕就是辣么酷 2024-08-18 01:23:49

递归多个单独的列表很混乱(当然,这可能是本练习想要的) - 通常更容易从它们两个中创建一个列表(“并排”,所以在您的情况下,每个条目都有一对整数),然后映射或折叠它。我怀疑 ML 会有一个名为“zip”的函数,它应该可以帮助您入门。

另外,这并不是你现在想要的,但如果你想要一本好的 ML 书(它实际上是 OCaml 的一种方言,但已经足够老了,与 SML 没有什么不同),那么看看 Cousineau + Mauny。如果你假期有时间的话也许可以。这是一本非常好的书——有点像 SICP,但是是针对 ML 的。

Recursing over multiple separate lists is messy (but might be what is wanted for this exercise, of course) - it's normally easier to make a single list from them both ("side by side", so in your case each entry would have a pair of integers) and then map or fold over that. I suspect ML will have a function called something like "zip" which should get you started.

Also, not really what you're looking for at the moment, but if you want a good ML book (it's actually a dialect of OCaml, but old enough to not be that different from SML) then look at Cousineau + Mauny. Maybe if you have some time in the holidays. It's a really good book - a bit like SICP, but for ML.

栩栩如生 2024-08-18 01:23:49

你明白什么是柯里化吗?

例如,您了解

fun compute(m,b,x):real = m*x+b ;

fun linearF (m,b) x : real = m*x+b ;

之间的区别吗?如果您了解,您能解释一下

val g = linearF(1.0,~1.0) ;

是什么吗?

柯里化并不是解决问题所必需的,但它是函数式编程中广泛使用的一种技术。特别是,如果您愿意,它为您提供了一种使用 List.map 的方法。在这里,您想要映射 list1,但要使用的函数需要了解 list2 和计算结果。这表明您的代码可能具有以下形式

List.map (f [] list2) list1

,其中 f 是某个适当定义的柯里化函数。一般来说,这是一个有用的技巧,可以为出于类似 List.map 的原因而需要成为“单参数”函数的函数提供更多信息。

这有帮助吗?

Do you understand what Currying is?

For instance, do you understand the difference between

fun compute(m,b,x):real = m*x+b ;

and

fun linearF (m,b) x : real = m*x+b ;

If you do, can you explain what

val g = linearF(1.0,~1.0) ;

does?

Currying is NOT strictly necessary for solving your problem, but it IS a technique that is used a lot in functional programming. In particular, it gives you a way to use List.map if you want. Here, you want to map over list1 but the function you want to use needs to know about list2 and the result of the computation. This suggests that your code might have the shape

List.map (f [] list2) list1

where f is some appropriately defined Curried function. In general this is a useful trick for providing more information to a function that needs to be a "one argument" function for List.map-like reasons.

Does this help?

爱情眠于流年 2024-08-18 01:23:49

考虑下面的map2代码,
这个函数的作用与map的作用完全相同,但有两个列表。

exception UnequalLengths;

fun map2(f,[],[]) = []
  | map2(f,(a::s),(a'::s')) = 
    (f(a,a'))::(map2(f,s,s'))
  | map2(f,s,s') = raise UnequalLengths;

fun plus(x,y) = x + y;
fun concat(s1,s2) = s1 ^ s2;

现在你可以像这样使用它:

- map2(plus,[1,2,3],[4,5,6]);
val it = [5,7,9] : int list
- map2(concat,["a","b","c"],["d","e","f"]);
val it = ["ad","be","cf"] : string list

享受XD

Consider this following code of map2,
this function does exactly what map does but with two lists.

exception UnequalLengths;

fun map2(f,[],[]) = []
  | map2(f,(a::s),(a'::s')) = 
    (f(a,a'))::(map2(f,s,s'))
  | map2(f,s,s') = raise UnequalLengths;

fun plus(x,y) = x + y;
fun concat(s1,s2) = s1 ^ s2;

Now you use it like this:

- map2(plus,[1,2,3],[4,5,6]);
val it = [5,7,9] : int list
- map2(concat,["a","b","c"],["d","e","f"]);
val it = ["ad","be","cf"] : string list

Enjoy XD

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