简体   繁体   English

haskell中包含2个函数调用的函数

[英]Functions in haskell that contain 2 function calls

If I have a function that needs to call 2 functions, how can that be done in Haskell?? 如果我有一个需要调用2个函数的函数,该如何在Haskell中完成?

For example if I need 2 functions to print the first 10 and last 10 items in a list, and i want to call both these functions from within one function 例如,如果我需要2个函数来打印列表中的前10个和最后10个项目,而我想从一个函数中调用这两个函数

i assume you have two functions of type 我假设你有两种类型的功能

f :: a -> IO ()

eg 例如

printFirst :: Show a => [a] -> IO ()
printLast :: Show a => [a] -> IO ()

then (because IO is a monad), you can do 然后(因为IO是monad),您可以

printBoth xs = printFirst xs >> printLast xs

or with syntactic sugar 或搭配语法糖

printBoth xs = do
    printFirst xs
    printLast xs

You probably want to combine the two function calls into a combined string (or list of items) and return them or output them in that same function. 您可能希望将两个函数调用组合成一个组合的字符串(或项目列表),然后将其返回或在同一函数中输出。 For instance, to print out the results of two functions: 例如,要打印出两个函数的结果:

print ("First function: " ++ (show (f 1)) ++ " second function: " ++ (show (f 2)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM