Try it! Haskell !

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!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市邑退,隨后出現(xiàn)的幾起案子纲刀,更是在濱河造成了極大的恐慌梅惯,老刑警劉巖畔塔,帶你破解...
    沈念sama閱讀 218,546評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件郎笆,死亡現(xiàn)場(chǎng)離奇詭異昌犹,居然都是意外死亡糟需,警方通過查閱死者的電腦和手機(jī)屉佳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)洲押,“玉大人武花,你說(shuō)我怎么就攤上這事¤菊剩” “怎么了体箕?”我有些...
    開封第一講書人閱讀 164,911評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)娘荡。 經(jīng)常有香客問我干旁,道長(zhǎng),這世上最難降的妖魔是什么炮沐? 我笑而不...
    開封第一講書人閱讀 58,737評(píng)論 1 294
  • 正文 為了忘掉前任争群,我火速辦了婚禮,結(jié)果婚禮上大年,老公的妹妹穿的比我還像新娘换薄。我一直安慰自己,他們只是感情好翔试,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評(píng)論 6 392
  • 文/花漫 我一把揭開白布轻要。 她就那樣靜靜地躺著,像睡著了一般垦缅。 火紅的嫁衣襯著肌膚如雪冲泥。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音凡恍,去河邊找鬼志秃。 笑死,一個(gè)胖子當(dāng)著我的面吹牛嚼酝,可吹牛的內(nèi)容都是我干的浮还。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼闽巩,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼钧舌!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起涎跨,我...
    開封第一講書人閱讀 39,249評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤洼冻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后六敬,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體碘赖,經(jīng)...
    沈念sama閱讀 45,696評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡驾荣,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評(píng)論 3 336
  • 正文 我和宋清朗相戀三年外构,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片播掷。...
    茶點(diǎn)故事閱讀 40,013評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡审编,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出歧匈,到底是詐尸還是另有隱情垒酬,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評(píng)論 5 346
  • 正文 年R本政府宣布件炉,位于F島的核電站勘究,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏斟冕。R本人自食惡果不足惜口糕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望磕蛇。 院中可真熱鬧景描,春花似錦、人聲如沸秀撇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)呵燕。三九已至棠绘,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背氧苍。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工适肠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人候引。 一個(gè)月前我還...
    沈念sama閱讀 48,203評(píng)論 3 370
  • 正文 我出身青樓侯养,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親澄干。 傳聞我的和親對(duì)象是個(gè)殘疾皇子逛揩,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容