美文网首页
Try it! Haskell !

Try it! Haskell !

作者: zzz雪人 | 来源:发表于2020-04-09 10:50 被阅读0次

An advanced, purely functional programming language

Lists and Tuples
You can only have a list of numbers or a list of characters, whereas in a tuple you can throw anything in!

We've also seen that you can make a new list with (:) that joins two values together, like:

λ 1 : [2,3]
[1,2,3]:: Num a => [a]

But we can't do this with tuples! You can only write a tuple and then look at what's inside. You can't make new ones on the fly like a list.

Let's write our own functions! It's really easy. How about something simple:

let square x = x * x in square 3

Let there be functions

Nice one! I think you're getting used to the let syntax.

You defined a function. You can read it as, as for a given parameter called x, square of x is x * x.

Some others you can try are:

let add1 x = x + 1 in add1 5
let second x = snd x in second (3,4)
Let's go crazy and use our square function with map:

let square x = x * x in map square [1..10]

λ let add1 x = x + 1 in add1 5
6:: Num a => a

λ let second x = snd x in second (3,4)
4:: Num b => b

λ let square x = x * x in map square [1..10]
[1,4,9,16,25,36,49,64,81,100]:: (Enum b, Num b) => [b]

Let there be functions

That's so cool! You described a simple function square and then you just passed it to another function (map) and got back [1,4,9,16,25,36,49,64,81,100], exactly what you expected!

Haskell is pretty good at composing things together like this. Some other things you can try are:

let add1 x = x + 1 in map add1 [1,5,7]
let take5s = filter (==5) in take5s [1,5,2,5,3,5]
let take5s = filter (==5) in map take5s [[1,5],[5],[1,1]]
Did you get back what you expected?

One more example for text; how do you upcase a letter?

toUpper 'a'

λ let add1 x = x + 1 in map add1 [1,5,7]
[2,6,8]:: Num b => [b]
λ let take5s = filter (==5) in take5s [1,5,2,5,3,5]
[5,5,5]:: (Eq a, Num a) => [a]
λ let take5s = filter (==5) in map take5s [[1,5],[5],[1,1]]
[[5],[5],[]]:: (Eq a, Num a) => [[a]]

Exercise time!

Easy! Remember: characters are written like 'a' and strings (lists of characters) are written like "a".

I need you to use toUpper capitalise my whole name, "Chris". Give it a try. You can do it, I believe in you!

Lesson 4 complete!

Brilliant! You're making excellent progress! You just passed toUpper to map. No problem.

Let's go over what you've learned in this lesson:

Functions like map take other functions as parameters.
Functions like (+1), (>5) and square can be passed to other functions.
Defining functions is just a case of writing what to do with the parameters.
Let's check out pattern matching; a way to get values from other values using patterns. Try this:

let (a,b) = (10,12) in a * 2

Ignorance is bliss

You're getting into tricky syntax, huh? I know you can handle it!

If you just want some of the values, you can ignore the others with _ (underscore) like this:

let (a:::_) = "xyz" in a

In fact, (a:b:c:d) is short-hand for (a:(b:(c:d))), so you can just ignore the rest in one go:

let (a:_) = "xyz" in a

λ let (a,b) = (10,12) in a * 2
20:: Num a => a
λ let (a:_:_:_) = "xyz" in a
'x':: Char
λ let (a:_) = "xyz" in a
'x':: Char

Show me the money!

Try to get the 'a' value from this value using pattern matching:

(10,"abc")

Perfetto!

Wizard! I think you've got pattern-matching down.

If you're still a bit unsure, here are some other things you can try:

let ::c:_ = "abcd" in c
let [a,b,c] = "cat" in (a,b,c)
You can also grab a whole value and pattern match on it (have your cake and eat it too):

let abc@(a,b,c) = (10,20,30) in (abc,a,b,c)

And that's the end of that chapter

That was easy, right?

Let's go over what you've learned in this lesson:

  1. Values are pattern matched, or deconstructed, by writing however they were constructed.
  2. Patterns let you use the values that you match.
  3. You can ignore whichever values you want.
  4. You can pattern match and keep hold of the original value too.

Okay! That's all for now. It's time to dig into some documentation!

相关文章

  • Try it! Haskell !

    An advanced, purely functional programming language Lists...

  • 函数式的宗教-00: 认识lisp(clojure)与haske

    总体大纲: lisp与haskell简单介绍 lisp与haskell应用领域 lisp与haskell技术分析 ...

  • monad以及monad的四条定理

    haskell的范畴是hask范畴(haskell的所有类型隶属于hask范畴),所以haskell的所有函子都是...

  • 01 数据类型

    swift中结构体在haskell中的描述 枚举类型在haskell中的描述 枚举携带类型在haskell中描述 ...

  • try try? try!

    try try? try! 也是好晕 如这样的情况 报错提示 需要加上 try, try是swift 2...

  • try  try  try

    2017年1月9日练了一节艾扬格小班课程,力量和精准练习,让我从胳膊酸到腿,身体累,但心情却很放松,夜里也深沉的睡...

  • try、try?、try!

    try: try与do-catch语句一起使用,并对错误进行详细处理。 try? try?用在错误无关紧要的时候,...

  • Haskell学习-函数式编程初探

    原文地址:Haskell学习-函数式编程初探  为什么要学习函数式编程?为什么要学习Haskell?  .net到...

  • Haskell

    [TOC] Haskell GHCI 通过Tab可以自动补全 通过 :browser 模块名称,浏览该模块下的函数...

  • haskell

    我在这里只是表达此刻内心想到的一些事情,或者记录自己关于最近学习生活工作的想法。 从我这一周对haskell的学习...

网友评论

      本文标题:Try it! Haskell !

      本文链接:https://www.haomeiwen.com/subject/lrbmuhtx.html