Haskell入门 [英] Getting started with Haskell

查看:123
本文介绍了Haskell入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来,我试图围绕Haskell中的函数式编程范例进行研究。我通过阅读教程和观看截屏视频来完成这一任务,但似乎没有任何关系。
现在,在学习各种命令/ OO语言(如C,Java,PHP)时,练习对我来说是一个好方法。但是由于我并不真正了解Haskell的能力,并且有许多新的概念可供使用,所以我不知道从哪里开始。



你学习了Haskell吗?是什么让你真的破冰?此外,任何好的想法开始练习?

解决方案

我要按照您的技能水平订购本指南哈斯克尔,从绝对的初学者到专家。请注意,这个过程需要几个月(几年?),所以它相当长。



绝对初学者 $ b

首先,haskell具备足够的技巧,可以做任何事情。它非常快(根据我的经验,仅在c和c ++之后),并且可以用于从模拟到服务器,guis和web应用程序的任何事情。



然而,哈斯克尔的初学者比其他人更容易写出问题。数学问题和列表处理程序是很好的候选对象,因为它们只需要最基本的haskell知识即可编写。

首先,学习haskell基础知识的一些很好的指南是快乐学习haskell教程以及向您学习haskell 的前6章。在阅读这些内容时,解决简单的问题也是一个很好的主意。 另外两个很好的资源是 Haskell从最基本的原则编程在Haskell编程。他们都为每章提供练习,所以你有一些简单的问题与你在前几页学到的内容相匹配。



一个很好的尝试列表是< a href =http://haskell.org/haskellwiki/H-99:_Ninety-Nine_Haskell_Problems =noreferrer> haskell 99问题页面。这些从非常基本的开始,随着你的继续变得更加困难。做很多这些练习是非常好的练习,因为他们让你练习递归和高阶函数的技巧。我建议跳过任何需要随机性的问题,因为这在Haskell中有点困难。如果您想使用QuickCheck测试您的解决方案,请查看这个SO问题(请参阅 Intermediate 下面)。



一旦你完成了其中的一部分,你可以继续做一些 Project Euler 问题。这些是根据有多少人完成了他们,这是一个很好的迹象表明困难。这些测试你的逻辑并比以前的问题更具价值,但你仍应该能够做到前几个。 haskell有一个很大的优势,就是整数的大小没有限制。为了完成这些问题中的一部分,阅读第7章和第8章学习haskell将会很有帮助。

初学者



之后,您应该对递归和更高阶的函数有相当好的处理,所以现在是开始做更多现实世界问题的好时机。一个非常好的开始是真实世界哈斯克尔(在线书籍,您也可以购买纸质版)。对于从未做过函数式编程/之前使用过递归的人,我发现前几章介绍得太快了。然而,通过练习以前的问题,你会发现它完全可以理解。

解决本书中的问题是学习如何管理抽象概念并在Haskell中构建可重用组件的好方法。这对于习惯于面向对象(oo)编程的人来说至关重要,因为正常的oo抽象方法(oo类)不出现在haskell中(haskell具有类型类,但它们与oo类非常不同,更像oo接口)。我认为跳过章节并不是一个好主意,因为每个章节都会介绍很多在后面的章节中使用的新想法。

过了一段时间,你会得到第14章,可怕的monads章节(dum dum dummmm)。由于概念的抽象程度,几乎每个学习haskell的人都难以理解单子。我无法想象另一种语言中的概念与函数式编程中的monad一样抽象。 Monads允许在一个想法下统一许多想法(例如IO操作,可能失败的计算,解析......)。所以,如果在阅读monads章节后,你不会感到气馁,那么你就不会真正理解它们。我发现阅读monads的许多不同解释是有用的;每一个问题都给出了一个新的视角。这是一个很好的 monad教程列表。我强烈推荐所有关于Monads ,但其他人也很好。

另外,这些概念需要一段时间才能真正沉入其中。这是通过使用,也是通过时间来实现的。我发现有时候睡在一个问题上比其他任何东西都有帮助!最终,这个想法会点击,你会想知道为什么你努力去理解一个实际上非常简单的概念。当这种情况发生的时候,它是非常棒的,当它发生时,你可能会发现haskell是你最喜欢的命令式编程语言:)为了确保你理解Haskell类型系统完美地,你应该尝试解决 20个中级Haskell练习。这些练习使用了毛茸茸和香蕉等功能的有趣名称,并帮助您对一些基本的功能性编程概念有了很好的理解,如果您还没有这些概念的话。您可以在这里度过一个愉快的夜晚,里面有箭头,独角兽,香肠和毛茸茸的香蕉等纸张。
$ b 中级



一旦你理解了Monads,我认为你已经完成了从初学者haskell程序员到中间haskeller的过渡。那么从哪里去?我会推荐的第一件事(如果你还没有从学习monads中学到它们的话)是各种类型的monads,比如Reader,Writer和State。再次,真实世界haskell和所有关于monads给这个很好的报道。要完成你的monad培训,了解monad变压器是必须的。这些让你可以将不同类型的Monads(如Reader和State monad)合并为一个。这看起来似乎毫无用处,但在使用它们一段时间之后,您会想知道如何在没有它们的情况下生活。



现在您可以完成真实世界的哈斯克尔书了想。现在跳过章节并没有什么关系,只要你有单身男人。只要选择你感兴趣的东西。



有了现在的知识,你应该可以使用cabal上的大部分软件包(至少有记录的软件包至少...)以及大多数随Haskell提供的库。有趣的图书馆列表可以尝试:


  • Parsec :用于解析程序和文本。比使用正则表达式好得多。优秀的文档,也有一个真正的世界haskell章节。

  • Quickcheck :一个非常酷的测试程序。你所做的是编写一个始终为真的谓词(例如 length(reverse lst)== length lst )。然后,您将谓词传递给quickCheck,并且它会生成很多随机值(在本例中为列表),并测试谓词对于所有结果是否为真。另请参阅在线手册


  • HUnit :在haskell中进行单元测试。


  • gtk2hs :最流行的haskell gui框架,让我们您在haskell中编写gtk应用程序。

  • happstack :一个用于haskell的web开发框架。不使用数据库,而使用数据类型存储。相当不错的文档(其他流行的框架将管理单元


此外,还有许多概念(如Monad概念),你最终应该学习。这将比第一次学习Monads容易,因为你的大脑将被用于处理涉及的抽象级别。了解这些高级概念以及它们如何结合在一起的非常好的概述是 Typeclassopedia




  • 应用:Monad这样的界面,但功能不那么强大。每个Monad都适用,但反之亦然。这是有用的,因为有些类型是Applicative但不是Monads。此外,使用Applicative函数编写的代码通常比使用Monad函数编写等效代码更加可组合。查看 Functors,Applicative Functors和Monoid 从你学习haskell指导。


  • 可折叠 Traversable :抽象列表的许多操作的类型类型,以便可以将相同的函数应用于其他容器类型。另请参阅 haskell wiki解释


  • Monoid :Monoid是一种具有零(或空值)值的类型,注释<> ,它们将两个幺半群连接在一起,使得 x<> mempty = mempty<> x = x x<> (y z)=(x′ y)· ž。这些被称为身份和结社性法律。许多类型是Monoid,例如数字,其中 mempty = 0 <> = + 。这在很多情况下都很有用。

  • 箭头:箭头是表示进行输入并返回输出的计算的一种方式。函数是最基本的箭头类型,但还有很多其他类型。该库还有许多非常有用的操作箭头的功能 - 即使只用于普通的旧式haskell函数,它们也非常有用。

    数组:haskell中的各种可变/不可变数组。


  • ST Monad :可让您编写具有可快速运行的可变状态的代码,同时仍保持纯粹的外部单子。 FRP:Functional Reactive Programming是一种新的编写处理事件,触发器,输入和输出的代码的实验性方法(比如,作为gui)。虽然我对此不太了解。 Paul Hudak关于yampa的谈话是一个好的开始。


>

你应该看看很多新的语言功能。我只列出它们,你可以从谷歌, haskell wikibook 找到很多关于它们的信息。 ,haskellwiki.org网站和 ghc文档




  • 多参数类型类/函数依赖关系

  • 类型族

  • 存在量化类型
  • 幻像类型

  • GADTS

  • 其他...



很多haskell基于类别理论,因此您可能需要查看进入那个。一个好的起点是计算机科学家的分类理论。如果你不想购买这本书,作者的相关文章也非常出色。



最后,您需要了解更多关于各种Haskell工具的信息。其中包括:


  • ghc (及其所有功能)

  • cabal : haskell包系统

  • darcs :用haskell编写的分布式版本控制系统,非常受哈斯克尔程序的欢迎。

  • haddock :haskell automatic文档生成器



在学习所有这些新的库和概念时,在haskell中编写一个中等大小的项目非常有用。它可以是任何东西(例如小游戏,数据分析器,网站,编译器)。在此基础上进行工作将允许您应用您正在学习的许多东西。你保持这个水平很长时间(这是我在哪里)。



专家



到这个阶段(2009年你好!)需要几年的时间,但是从这里开始,我猜你会开始写博士论文,新的ghc扩展,并且提出新的抽象概念。



获得帮助



最后,在学习的任何阶段,信息。它们是:


  • #haskell irc频道

  • 邮件列表。这些值得注册,只是为了阅读发生的讨论 - 有些非常有趣。

  • 在haskell.org主页上列出的其他地方



结论

好吧,这比我预期的要长...不管怎样,我认为精通haskell是一个非常好的主意。这需要很长时间,但主要是因为你正在通过这样学习一种全新的思维方式。它不像学习java之后学习ruby,而是喜欢学习java之后学习java。另外,我发现由于学习了haskell,我的面向对象的编程技巧得到了改进,因为我看到了许多抽象思路的新方法。


For a few days I've tried to wrap my head around the functional programming paradigm in Haskell. I've done this by reading tutorials and watching screencasts, but nothing really seems to stick. Now, in learning various imperative/OO languages (like C, Java, PHP), exercises have been a good way for me to go. But since I don't really know what Haskell is capable of and because there are many new concepts to utilize, I haven't known where to start.

So, how did you learn Haskell? What made you really "break the ice"? Also, any good ideas for beginning exercises?

解决方案

I'm going to order this guide by the level of skill you have in haskell, going from an absolute beginner right up to an expert. Note that this process will take many months (years?), so it is rather long.

Absolute Beginner

Firstly, haskell is capable of anything, with enough skill. It is very fast (behind only c and c++ in my experience), and can be used for anything from simulations to servers, guis and web applications.

However there are some problems that are easier to write for a beginner in haskell than others. Mathematical problems and list process programs are good candidates for this, as they only require the most basic of haskell knowledge to be able to write.

Firstly, some good guides to learning the very basics of haskell are the happy learn haskell tutorial and the first 6 chapters of learn you a haskell. While reading these, it is a very good idea to also be solving simple problems with what you know.

Another two good resources are Haskell Programming from first principles, and Programming in Haskell. They both come with exercises for each chapter, so you have small simple problems matching what you learned on the last few pages.

A good list of problems to try is the haskell 99 problems page. These start off very basic, and get more difficult as you go on. It is very good practice doing a lot of those, as they let you practice your skills in recursion and higher order functions. I would recommend skipping any problems that require randomness as that is a bit more difficult in haskell. Check this SO question in case you want to test your solutions with QuickCheck (see Intermediate below).

Once you have done a few of those, you could move on to doing a few of the Project Euler problems. These are sorted by how many people have completed them, which is a fairly good indication of difficulty. These test your logic and haskell more than the previous problems, but you should still be able to do the first few. A big advantage haskell has with these problems is Integers aren't limited in size. To complete some of these problems, it will be useful to have read chapters 7 and 8 of learn you a haskell as well.

Beginner

After that you should have a fairly good handle on recursion and higher order functions, so it would be a good time to start doing some more real world problems. A very good place to start is Real World Haskell (online book, you can also purchase a hard copy). I found the first few chapters introduced too much too quickly for someone who has never done functional programming/used recursion before. However with the practice you would have had from doing the previous problems you should find it perfectly understandable.

Working through the problems in the book is a great way of learning how to manage abstractions and building reusable components in haskell. This is vital for people used to object-orientated (oo) programming, as the normal oo abstraction methods (oo classes) don't appear in haskell (haskell has type classes, but they are very different to oo classes, more like oo interfaces). I don't think it is a good idea to skip chapters, as each introduces a lot new ideas that are used in later chapters.

After a while you will get to chapter 14, the dreaded monads chapter (dum dum dummmm). Almost everyone who learns haskell has trouble understanding monads, due to how abstract the concept is. I can't think of any concept in another language that is as abstract as monads are in functional programming. Monads allows many ideas (such as IO operations, computations that might fail, parsing,...) to be unified under one idea. So don't feel discouraged if after reading the monads chapter you don't really understand them. I found it useful to read many different explanations of monads; each one gives a new perspective on the problem. Here is a very good list of monad tutorials. I highly recommend the All About Monads, but the others are also good.

Also, it takes a while for the concepts to truly sink in. This comes through use, but also through time. I find that sometimes sleeping on a problem helps more than anything else! Eventually, the idea will click, and you will wonder why you struggled to understand a concept that in reality is incredibly simple. It is awesome when this happens, and when it does, you might find haskell to be your favorite imperative programming language :)

To make sure that you are understanding Haskell type system perfectly, you should try to solve 20 intermediate haskell exercises. Those exercises using fun names of functions like "furry" and "banana" and helps you to have a good understanding of some basic functional programming concepts if you don't have them already. Nice way to spend your evening with list of paper covered with arrows, unicorns, sausages and furry bananas.

Intermediate

Once you understand Monads, I think you have made the transition from a beginner haskell programmer to an intermediate haskeller. So where to go from here? The first thing I would recommend (if you haven't already learnt them from learning monads) is the various types of monads, such as Reader, Writer and State. Again, Real world haskell and All about monads gives great coverage of this. To complete your monad training learning about monad transformers is a must. These let you combine different types of Monads (such as a Reader and State monad) into one. This may seem useless to begin with, but after using them for a while you will wonder how you lived without them.

Now you can finish the real world haskell book if you want. Skipping chapters now though doesn't really matter, as long as you have monads down pat. Just choose what you are interested in.

With the knowledge you would have now, you should be able to use most of the packages on cabal (well the documented ones at least...), as well as most of the libraries that come with haskell. A list of interesting libraries to try would be:

  • Parsec: for parsing programs and text. Much better than using regexps. Excellent documentation, also has a real world haskell chapter.

  • Quickcheck: A very cool testing program. What you do is write a predicate that should always be true (eg length (reverse lst) == length lst). You then pass the predicate the quickCheck, and it will generate a lot of random values (in this case lists) and test that the predicate is true for all results. See also the online manual.

  • HUnit: Unit testing in haskell.

  • gtk2hs: The most popular gui framework for haskell, lets you write gtk applications in haskell.

  • happstack: A web development framework for haskell. Doesn't use databases, instead a data type store. Pretty good docs (other popular frameworks would be snap and yesod).

Also, there are many concepts (like the Monad concept) that you should eventually learn. This will be easier than learning Monads the first time, as your brain will be used to dealing with the level of abstraction involved. A very good overview for learning about these high level concepts and how they fit together is the Typeclassopedia.

  • Applicative: An interface like Monads, but less powerful. Every Monad is Applicative, but not vice versa. This is useful as there are some types that are Applicative but are not Monads. Also, code written using the Applicative functions is often more composable than writing the equivalent code using the Monad functions. See Functors, Applicative Functors and Monoids from the learn you a haskell guide.

  • Foldable,Traversable: Typeclasses that abstract many of the operations of lists, so that the same functions can be applied to other container types. See also the haskell wiki explaination.

  • Monoid: A Monoid is a type that has a zero (or mempty) value, and an operation, notated <> that joins two Monoids together, such that x <> mempty = mempty <> x = x and x <> (y <> z) = (x <> y) <> z. These are called identity and associativity laws. Many types are Monoids, such as numbers, with mempty = 0 and <> = +. This is useful in many situations.

  • Arrows: Arrows are a way of representing computations that take an input and return an output. A function is the most basic type of arrow, but there are many other types. The library also has many very useful functions for manipulating arrows - they are very useful even if only used with plain old haskell functions.

  • Arrays: the various mutable/immutable arrays in haskell.

  • ST Monad: lets you write code with a mutable state that runs very quickly, while still remaining pure outside the monad. See the link for more details.

  • FRP: Functional Reactive Programming, a new, experimental way of writing code that handles events, triggers, inputs and outputs (such as a gui). I don't know much about this though. Paul Hudak's talk about yampa is a good start.

There are a lot of new language features you should have a look at. I'll just list them, you can find lots of info about them from google, the haskell wikibook, the haskellwiki.org site and ghc documentation.

  • Multiparameter type classes/functional dependencies
  • Type families
  • Existentially quantified types
  • Phantom types
  • GADTS
  • others...

A lot of haskell is based around category theory, so you may want to look into that. A good starting point is Category Theory for Computer Scientist. If you don't want to buy the book, the author's related article is also excellent.

Finally you will want to learn more about the various haskell tools. These include:

  • ghc (and all its features)
  • cabal: the haskell package system
  • darcs: a distributed version control system written in haskell, very popular for haskell programs.
  • haddock: a haskell automatic documentation generator

While learning all these new libraries and concepts, it is very useful to be writing a moderate-sized project in haskell. It can be anything (eg a small game, data analyser, website, compiler). Working on this will allow you to apply many of the things you are now learning. You stay at this level for ages (this is where I'm at).

Expert

It will take you years to get to this stage (hello from 2009!), but from here I'm guessing you start writing phd papers, new ghc extensions, and coming up with new abstractions.

Getting Help

Finally, while at any stage of learning, there are multiple places for getting information. These are:

  • the #haskell irc channel
  • the mailing lists. These are worth signing up for just to read the discussions that take place - some are very interesting.
  • other places listed on the haskell.org home page

Conclusion

Well this turned out longer than I expected... Anyway, I think it is a very good idea to become proficient in haskell. It takes a long time, but that is mainly because you are learning a completely new way of thinking by doing so. It is not like learning ruby after learning java, but like learning java after learning c. Also, I am finding that my object-orientated programming skills have improved as a result of learning haskell, as I am seeing many new ways of abstracting ideas.

这篇关于Haskell入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆