《Two Dozen Short Lessons in Haskell》學習(十八) - 交互式鍵盤輸入和屏幕輸出

《Two Dozen Short Lessons in Haskell》(Copyright ? 1995, 1996, 1997 by Rex Page,有人翻譯為Haskell二十四學時教程,該書如果不用于贏利,可以任意發布,但需要保留他們的copyright)這本書是學習 Haskell的一套練習冊,共有2本,一本是問題,一本是答案,分為24個章節。在這個站點有PDF文件。幾年前剛開始學習Haskell的時候,感覺前幾章還可以看下去,后面的內容越來越難以理解。現在對函數式編程有了一些了解后,再來看這些題,許多內容變得簡單起來了。

初學Haskell之前一定要記住:

把你以前學習面向過程的常規的編程語言,如Pascal、C、Fortran等等統統忘在腦后,函數式編程完全是不一樣的編程模型,用以前的術語和思維來理解函數式編程里的概念,只會讓你困惑和迷茫,會嚴重地影響你的學習進度。

這個學習材料內容太多,想把整書全面翻譯下來非常困難,只有通過練習題將一些知識點串起來,詳細學習Haskell還是先看其它一些入門書籍吧,這本書配套著學學還是不錯的。

第18章?Interactive Keyboard Input and Screen Output

IO類型在初學Haskell的時候是一個很難理解的概念,平常的編程語言中已經習慣了輸入、輸出語句,但在函數式編程中一切皆函數,一個確定的函數會得到確定的計算結果,而與操作系統交互時函數式編程就不太方便了,這時Haskell引出了一個IO類型。

一個重要的do表達式:

這一章還介紹了一個字符串連接的函數,可以把字符串末尾加上換行符,再連接起來

unlines :: [String] -> String

unlines = concat . map (++ "\n")

例如:

unlines ["line1", "line2", "line3"]?=?"line1\nline2\nline3\n"

?

1 Values of IO type

a are in the equality class Eq

b specify requests for operating system services

c represent tuples in a unique way

d describe Jovian satellites?

?

2 Which of the following intrinsic functions in Haskell causes output to appear on the screen?

a concat :: [[any]] -> [any]

b putStr :: String -> IO ()

c printString :: Message –> Screen

d getLine :: IO String?

?

3 What will be the effect of the command main, given the following script?

HASKELL DEFINITION ?main =

HASKELL DEFINITION ??? do putStr "Good "

HASKELL DEFINITION ???????? putStr "Vibrations\n"

HASKELL DEFINITION ???????? putStr " by the Beach Boys\n"

a one line displayed on screen

b two lines displayed on screen

c three lines displayed on screen

d audio effects through the speaker?

?

4 What will be the effect of the command main, given the following script?

HASKELL DEFINITION ? main =

HASKELL DEFINITION ??? do putStr "Please enter your first and last name (e.g., John Doe): "

HASKELL DEFINITION ???????? firstLast <- getLine

HASKELL DEFINITION ???????? putStr (reverse firstLast)

a display of name entered, but with the last name first

b display of last name only, first name ignored

c display of last name only, spelled backwards

d display of name spelled backwards (書中在這里有印刷錯誤)?

?

5 How should the last input/output directive in the preceding question be changed to display the first name only?

什么時候只輸出名字?

a putStr(take 1 firstLast)

b putStr(drop 1 firstLast)

c putStr(takeWhile (/= ’ ’) firstLast)

d putStr(dropWhile (/= ’ ’) firstLast)?

?

=========================================================

=========================================================

1 b?

?

2 b

putStr函數返回的類型是IO (),表示要與操作系統有交互動作,這個()表示不返回任何數據。

?

3 b

結果應該是:

Good?Vibrations

?by the Beach Boys

?

4 d

如果輸入是John Doe

則屏幕輸出:eoD nhoJ

?

5 c

如果輸入是John Doe

選項a:take 1 firstLast,會取出第一個字符,“J"?

選項b:drop 1 firstLast,會除掉第一個字符, "ohn Doe"

選項c:takeWhile (/=' ') firstLast,會得到空格前的字符串,"John",是正確答案。

選項d:dropWhile (/=' ') firstLast,會除掉第一個空格前的所有字符," Doe",注意Doe前面還有一個空格?

轉載于:https://www.cnblogs.com/speeding/archive/2013/03/23/2976288.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274315.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274315.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274315.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

figma設計_Figma中簡單,可重復使用的設計系統

figma設計Putting together a design system may seem like an unnecessary hassle. It’s often easier to jump straight into designing things without having to worry about styles, components, or libraries. Some might even argue that when it comes to smaller pro…

WPF 關于鼠標事件和坐標

Mouse.GetPosition(window);可以在任何時間獲得相對任意元素的鼠標位置 Mouse.Capture(el);可以讓某個元素獲得所有的鼠標事件不管他應不應該的到鼠標事件 Mouse.Capture(null);解除轉載于:https://www.cnblogs.com/wangjixianyun/archive/2013/03/25/2980953.html

訪問25%無法訪問的人-如何設計可訪問性

We’re increasingly dependent on the internet and computers for everything we do — this has become starkly more obvious through the COVID19 global pandemic.我們所做的一切都越來越依賴于互聯網和計算機-通過COVID19全球大流行&#xff0c;這一點變得更加明顯。 F…

DDD:實體如何處理外部依賴

場景 修改用戶名時&#xff0c;要驗證用戶名的唯一性。 實現1 1 public class User 2 { 3   public void ChangeUsername(string newUsername) 4   { 5   //使用服務定位器獲取IUsernameUniqueService &#xff0c;執行驗證。 6   } 7 } 實現…

架構師論壇 創業_我在早期創業時作為設計師學到的東西

架構師論壇 創業For over 2 years at a young product company, I collaborated with talented engineering folks for a 0 → 1 suite of products. Here are my learnings and key takeaways. Of course, these are my views and do not represent those of my employers or …

HFileOutputFormat與TotalOrderPartitioner

最近需要為一些數據增加隨機讀的功能&#xff0c;于是采用生成HFile再bulk load進HBase的方式。 運行的時候map很快完成&#xff0c;reduce在sort階段花費時間很長&#xff0c;reducer用的是KeyValueSortReducer而且只有一個&#xff0c;這就形成了單reducer全排序的瓶頸。于是…

qt按鈕禁用和激活禁用_為什么試探法只是經驗法則:禁用按鈕的情況

qt按鈕禁用和激活禁用Most user experience designers will be familiar with Jackob Nielsen’s 10 usability heuristics. They are widely cited and a great set of broad rules of thumb to follow when designing user interfaces.大多數用戶體驗設計師將熟悉Jackob Niel…

Teach Yourself Java 2 in 21 Days 書中樣例代碼實踐

找了好幾書JAVA的書&#xff0c;看了幾章&#xff0c;都看不下去。 我覺得適合《Teach Yourself Java 2 in 21 Days》&#xff08;Rogers Cadenhead Laura Lemay&#xff09;還是適合我的。 孫衛琴那本&#xff0c;我感覺就羅嗦多了沒到我點子上。 接口&#xff0c;抽象類這些內…

好奇心機制_好奇心問題

好奇心機制For my past two jobs I’ve posted a question every week in my team chat and learned so much about my co-workers. Give it a try! :D對于過去的兩個工作&#xff0c;我每周都會在團隊聊天中發布一個問題&#xff0c;并且對我的同事了解很多。 試試看&#xff…

20130328java基礎學習筆記-循環結構for以及for,while循環區別

1.循環結構:for講解class ForDemo{ public static void main(String[] args) { /* for(初始化表達式;循環條件表達式;循環后的操作表達式) { 執行語句;(循環體) } */ for(int x 1; x<3; x) { …

小程序設計避免犯什么錯_新設計師犯下的5種印刷錯誤以及如何避免

小程序設計避免犯什么錯Over the last year and a half, I’ve had the opportunity to teach the basics of typography to undergraduate graphic design students. During this time, I’ve noticed some common mistakes that my students make when first learning how to…

移動設備web文字單位_移動設備如何塑造現代Web設計

移動設備web文字單位I was working with a nonprofit earlier this month on redesigning their website and during the first meeting, I proposed a very standard idea: the home page needed to tell a story and guide the intended user through the intended process (…

hp-ux修改時區方法_UX研究人員可以倡導人類的6種方法

hp-ux修改時區方法In the UX world, we often hear terms like “user-centered,” “human-centered,” and “customer-centered.” We believe that in order to be innovative, we need to center experiences that are authentic, intuitive, and practical.在UX世界中&am…

2013年3月百度之星A題

偽隨機數生成器 題目描述 baidu熊最近在學習隨機算法&#xff0c;于是他決定自己做一個隨機數生成器。 這個隨機數生成器通過三個參數c, q, n作為種子, 然后它就可以通過以下方式生成偽隨機數序列&#xff1a; m0 c, mi1 (q2mi 1) mod 2n, for all i > 0. 因為一些奇怪的…

為什么張揚的人別人很討厭_為什么每個人總是討厭重新設計,即使他們很好

為什么張揚的人別人很討厭重點 (Top highlight)微處理 (Microprocessing) In Microprocessing, columnist Angela Lashbrook aims to improve your relationship with technology every week. Microprocessing goes deep on the little things that define your online life to…

轉載--C語言:浮點數在內存中的表示

單精度浮點數&#xff1a; 1位符號位 8位階碼位 23位尾數 雙精度浮點數&#xff1a; 1位符號位 8位階碼位 52位尾數 實數在內存中以規范化的浮點數存放&#xff0c;包括數符、階碼、尾數。數的精度取決于尾數的位數。比如32位機上float型為23位 double型為52位。…

學習ui設計_如果您想學習UI設計,該怎么辦

學習ui設計There is a question that is always asked when we want to learn something new.當我們想學習新東西時&#xff0c;總會問一個問題。 Where to start?從哪兒開始&#xff1f; This is also being my question when I want to learn UI design. In this article, …

Christmas

html5 game - Christmasloading......轉載于:https://www.cnblogs.com/yorhom/archive/2013/04/05/3001116.html

30個WordPress Retina(iPad)自適應主題

原文地址&#xff1a;http://www.goodfav.com/zh/retina-ready-wordpress-themes-3556.html WordPress Retina定制主題進行了優化&#xff0c;支持Retina屏幕上的高品質和清晰的圖像。如果你關心這個話題&#xff0c;又不知道這究竟是什么&#xff0c;那么請你繼續閱讀。 wordp…

Thinking in java第一章對象導論

這一章&#xff0c;做筆記感覺不是很好做。每個人又每個人對面向對象的理解。這里說一下書里的關鍵字&#xff0c;穿插一下自己的思想 面向對象的編程語言里面很流行的一句話&#xff0c;一切都是對象。面向對象的核心就是抽象&#xff0c;抽象的能力有大有小&#xff0c;是決定…