`

haskell - Input and Output - helloworld

阅读更多

We've mentioned that Haskell is a purely functional language. Whereas in imperative languages you usually get things done by giving the computer a series of steps to execute, functional programming is more of defining what stuff is. In Haskell, a function can't change some state, like changing the contents of a variable (when a function changes state, we say that the function has side-effects). The only thing a function can do in Haskell is give us back some result based on the parameters we gave it. If a function is called two times with the same parameters, it has to return the same result. While this may seem a bit limiting when you're coming from an imperative world, we've seen that it's actually really cool. In an imperative language, you have no guarantee that a simple function that should just crunch some numbers won't burn down your house, kidnap your dog and scratch your car with a potato while crunching those numbers. For instance, when we were making a binary search tree, we didn't insert an element into a tree by modifying some tree in place. Our function for inserting into a binary search tree actually returned a new tree, because it can't change the old one.

 

 It turns out that Haskell actually has a really clever system for dealing with functions that have side-effects that neatly separates the part of our program that is pure and the part of our program that is impure, which does all the dirty work like talking to the keyboard and the screen. With those two parts separated, we can still reason about our pure program and take advantage of all the things that purity offers, like laziness, robustness and modularity while efficiently communicating with the outside world.

 

what will be covered by this post includes the following. 

  • hello world and how to run the hello world program 
  • type of putStrLn and getLine
  • the do monad, an introduction 
  • IO binding such as name <- getLine, don't confuse this with the name = getLine
  • let binding in the do monads
  • continuous reading from console - recursion and forever
  • every if has a matching else - if condition then I/O else I/O action 
  • return statement in the do monads
  • putChar, putStr, and print, getChar
  • when, sequence and mapM and mapM_.
  • forver, and forM (to create I/O action)

 

Helloworld and run haskell program 

We will start by inspecting the helloworld program . 

 

a very simple hello world program is as such .

main = putStrLn "hello, world"  

 

you generally can run the applicaiton in two ways. 

1. compile the code to native code, and run the native code. 

$ ghc --make helloworld  
[1 of 1] Compiling Main             ( helloworld.hs, helloworld.o )  
Linking helloworld ...  

 and then you run it 

$ ./helloworld  
hello, world  

 2. run it as a script 

runhaskell helloworld.hs

 

type of putStrLn and getLine

If we check the type of putStrLn and getLine, 

 

putStrLn

 

ghci> :t putStrLn  
putStrLn :: String -> IO ()  
ghci> :t putStrLn "hello, world"  
putStrLn "hello, world" :: IO ()  

 

 

which means that the putStrLn takes a string, and returns an IO action which has a result type of ()((i.e. the empty tuple, also know as unit).). 

getLine

 

ghci> :t getLine  
getLine :: IO String  

 what does this mean getLine is an I/O action that contains a result type of String. 

 

 

then let's see how we can use the do nomand and the I/O binding. here is the code.

 

 

main = do 
   putStrLn "Hello, what's your name?"
   name <- getLine
   putStrLn("Hey " ++ name ++ ",you rock!")

 

 

the code name <- getLine, is get the resut of the getLine I/O action and return the values to name, it is different to the following. 

 

nameTag = "Hello, my name is "  ++ getLine

 only gives a name to the I/O action. 

 

 

Let binding in do block 

We have covered in pattern matching that we can use the let...in... binding. we can use the "let" binding as well. here is the code .

 

 

import Data.Char

main = do 
   putStrLn "What's your first name?"
   firstName <- getLine
   putStrLn "What's  your last name ?"
   lastName <- getLine 
   let bigFirstName = map toUpper firstName
       bigLastName = map toUpper lastName
   putStrLn $ "hey " ++ bigFirstName ++ " " ++ bigLastName ++ ", how are you?" 

 

 

If we want to achive something that continous reading from the command line, an example is as follow, (we will examine the problem again when we coveres the forever monad)

 

main = do   
    line <- getLine  
    if null line  
        then return ()  
        else do  
            putStrLn $ reverseWords line  
            main  
  
reverseWords :: String -> String  
reverseWords = unwords . map reverse . words  

 

 

in the example above the code continous read from the same console, and then put the result to the console.

 

while you can do the same things with the forever, 

 

-- file 
--  forever
-- descrpition: 
--    takes an I/O action and returns an I/O action that just repeats the I/O action it got forever. It's located in Control.Monad

import Control.Monad  
import Data.Char  
  
main = forever $ do  
    putStr "Give me some input: "  
    l <- getLine  
    putStrLn $ map toUpper l  

 

if-else should be paired

 

 

We might have noticed that we have written with every if statement an else statement, the reason why?emember that in Haskell, every if must have a corresponding else because every expression has to have some sort of value. We make the if so that when a condition is true (in our case, the line that we entered is blank), we perform one I/O action and when it isn't, the I/O action under the else is performed. That's why in an I/O do block, ifs have to have a form of if condition then I/O action else I/O action.

 

Let's see some examples which has one return statement. 

 

-- file 
--   return_haskell.hs
-- description:
--   dive into the return statement

-- return do not cause the block to end
main = do  
    return ()  
    return "HAHAHA"  
    line <- getLine  
    return "BLAH BLAH BLAH"  
    return 4  
    putStrLn line  

-- return is like the opposite of <- 

main = do  
    a <- return "hell"  
    b <- return "yeah!"  
    putStrLn $ a ++ " " ++ b  

-- return can be replaced by the let binding, so sometimes the return statement is redundant

main = do  
    let a = "hell"  
        b = "yeah"  
    putStrLn $ a ++ " " ++ b  

 

 

putStr,putChar and print. 

 

then we will examine the putStr,putChar and print. 

 

 

main = do  putStr "Hey, "
           putStr "I'm "
           putStrLn "Any!"

 

 

 

--- it is maybe anarchasim

main = do putChar 't'
          putChar 'e'
          putchar 'h'

 and 

 

 

main = do print True 
          print 2
          print "haha"
          print 3.2
          print [3, 4, 3]

 

 

so, putStr print a string, putChar print a single character, and print will takes a type which is instance of Show, and then print the representation . 

 

and below is the getChar call. 

 

main = do 
   c <- getChar
   if c /= ' ' 
      then do 
        putChar c 
        main 
       else return ()

 

 

When and sequence 

when and sequence are calls which are from Control.Monad, it gives flow control, but they are actually some functions., 

examples are as below. 

-- file 
-- when_monad.hs
-- descrpition: 
--   the Control.Monad

import Control.Monad

main =do 
   c <- getChar
   when (c  /= ' ') $ do 
      putChar c 
      main

 and Sequence function. 

-- file 
--  sequence_io.hs
-- descrpition: 
--   the sequence  takes a list of I/O actions and returns an I/O actions that will perform those actions one after the other. 


-- main = do 
--     a <- getLine
--     b <- getLine  
--     c <- getLine  
--     print [a,b,c] 


main = do 
   rs <- sequence [getLine, getLine, getLine]
   print rs


-- below won't create an I/O action, it will create a list of I/O actions, because that is like writing 
-- map print [1, 2, 3, 4]
-- it is like 
-- [print 1, print 2, print 3, print 4]
sequence (map print [1,2,3,4,5])

 

mapM and mapM_

 

mapM takes a function and a list, maps the function over the list and then sequences it.mapM_ does the same, only it throws away the result later. We usually use mapM_ when we don't care what result our sequenced I/O actions have.

 

ghci> mapM print [1,2,3]  
1  
2  
3  
[(),(),()]  
ghci> mapM_ print [1,2,3]  
1  
2  
3  

 

forever

we have discussed the forever function, we will skip this one for now. 

 

forM

-- file 
--  forM_io.hs
-- descrpition: 
--   (located in Control.Monad) is like mapM, only that it has its parameters switched around.

import Control.Monad  

main = do   
    colors <- forM [1,2,3,4] (\a -> do  
        putStrLn $ "Which color do you associate with the number " ++ show a ++ "?"  
        color <- getLine  
        return color)  
    putStrLn "The colors that you associate with 1, 2, 3 and 4 are: "  
    mapM putStrLn colors  

 

so it basically create an IO actions, whose result we bind to colors. and we can do some action like print on the I/O actions. 

 

 

 

 

分享到:
评论

相关推荐

    Atom-ide-haskell-hoogle,在光标下显示符号的滚动信息。对原子的贡献.zip

    Atom-ide-haskell-hoogle 是一个专门为 Atom 编辑器设计的插件,它整合了 Haskell 的 Hoogle 工具,以提供强大的代码提示和搜索功能。Atom 是一款由 GitHub 开发的开源文本编辑器,它利用现代 web 技术如 HTML、CSS ...

    haskell-mode emacs

    在 Emacs 中,`haskell-mode` 是一个专门为了提升 Haskell 开发体验而设计的模式。 `haskell-mode` 提供了多种增强功能,旨在帮助 Haskell 开发者更高效地编写、调试和理解代码。这个模式包括以下关键特性: 1. **...

    Atom-haskell-ghc-mod,哈斯克尔.zip

    **哈斯克尔编程语言与Atom-Haskell-GHC-Mod** 哈斯克尔(Haskell)是一种纯函数式编程语言,以其优雅的语法、强静态类型系统和编译时优化而受到程序员的喜爱。它鼓励使用不可变数据和惰性求值,这使得哈斯克尔在...

    haskell-chart, haskell的2D 图表库.zip

    在数据可视化领域,`haskell-chart`库提供了一种高效且灵活的方式来创建2D图表,这对于数据分析、科学计算以及教学等场景非常有用。这个库是开源的,意味着任何人都可以查看其源代码,学习并贡献改进。 `haskell-...

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码.zip

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...

    Atom-haskell-debug,使用ghci在atom中实现图形haskell调试器.zip

    Atom-Haskell-Debug是针对Haskell开发者的一个强大工具,它允许你在流行的Atom文本编辑器中集成一个图形化的Haskell调试器。这个工具基于GHCi(Glasgow Haskell Compiler Interface),GHCi是Haskell的交互式环境,...

    haskell-ghc-mod:haskell-ghc-mod原子包

    从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...

    haskell-ghc-mod:haskell-ghc-mod原子包

    haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...

    Get Programming with HASKELL-2018-英文版.pdf

    Get Programming with HASKELL-2018-英文版

    Atom-atom-haskell-scry,扩散系数.zip

    【标题】:“Atom-atom-haskell-scry,扩散系数.zip” 涉及的主要知识点是 Atom 编辑器与 Haskell 语言的集成以及 SCRY 工具的使用。 【描述】:“Atom-atom-haskell-scry.zip”的描述指出,这个压缩包包含了一个名...

    haskell-relational-record-driver-mysql:用于 haskell-relational-record 的 MySQL 驱动程序

    用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...

    Atom-ide-haskell-repl,原子中的ghci repl。对原子的贡献.zip

    Atom-ide-haskell-repl是针对Atom文本编辑器的一个扩展插件,专为Haskell编程语言提供集成的GHCi(Glasgow Haskell Compiler Interface)交互式环境,即REPL(Read-Eval-Print Loop)。这个插件允许开发者在Atom编辑...

    haskell-dev-tools:我用来安装升级与Haskell相关的开发工具的元项目

    在Haskell的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...

    haskell-dap:Haskell DAP接口数据的实现

    Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...

    函数式编程-haskell-to-java

    ### 函数式编程:Haskell到Java的转换 #### 概述 本文旨在探讨函数式编程语言Haskell如何被编译或转换为Java语言。Haskell作为一种纯函数式编程语言,以其强大的类型系统、惰性求值机制以及高度抽象的能力在学术界...

    haskell-formatter:Haskell源代码格式化程序-Form source code

    haskell-formatter --force --input a.hs --output a.hs hindent a.hs 就地格式化多个文件 hindent a.hs b.hs 将stdin格式化为stdout haskell-formatter hindent 订单进口 完成 hindent --sort-imports … ...

    Atom-ide-haskell-cabal,IDE的Cabal后端提供商.zip

    Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。

    haskell-tools:Haskell的开发人员工具

    "haskell-tools"就是这样一个项目,它专注于为Haskell开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...

    Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid

    ### Haskell - The Craft of Functional Programming #### 一、概述 《Haskell - The Craft of Functional Programming》是一本深入探讨Haskell编程语言的经典著作。本书由Tantanoid编写,并于1999年由Addison-...

    haskell-brainfuck:Haskel 脑残翻译

    你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...

Global site tag (gtag.js) - Google Analytics