Haskell程式:確定作業系統名稱和版本


本教程討論瞭如何在Haskell程式語言中編寫程式來查詢作業系統資訊。

Haskell提供了查詢系統資訊的函式。這些函式可以透過從Haskell的System包中匯入Info模組來訪問。

在本教程中,我們將看到:

  • 顯示作業系統名稱的程式。
  • 顯示處理器架構的程式。
  • 顯示編譯器名稱的程式。
  • 顯示編譯器版本的程式。

語法

在Haskell中匯入模組的語法如下。

import packageName.moduleName

要從system包中匯入Info模組,語法如下。

import System.Info

現在,Info模組中的所有函式都可以被程式訪問。

示例1

顯示作業系統名稱的程式

-- importing Info module from System package
import System.Info
main = do
-- printing the name of the operating system
   print ("Name of the operating system is:")
   print(os)

輸出

"Name of the operating system is:"
"linux"

在上面的程式中,我們從system包中匯入了Info模組。在main函式中,我們呼叫了一個函式os (os :: [Char]),它是Info模組中返回作業系統名稱的內建函式。最後,使用print函式列印返回的輸出。

示例2

顯示處理器架構的程式。

-- importing Info module from System package
import System.Info
main = do
-- printing the architecture of the processor
   print ("The architecture of the processor is:")
   print(arch)

輸出

"The architecture of the processor is:"
"x86_64"

在上面的程式中,我們從System包中匯入了Info模組。在main函式中,我們呼叫了一個函式arch (arch :: [Char]),它是Info模組中返回處理器架構名稱的內建函式。最後,使用print函式列印返回的輸出。

示例3

顯示編譯器名稱的程式。

-- importing Info module from System package
import System.Info
main = do
-- printing the name of the compiler
   print ("The name of the compiler is:")
   print(compilerName)

輸出

"The name of the compiler is:"
"ghc"

在上面的程式中,我們從System包中匯入了Info模組。在main函式中,我們呼叫了一個函式compilerName (compilerName :: [Char]),它是Info模組中返回用於編譯程式碼的編譯器名稱的內建函式。最後,使用print函式列印返回的輸出。

示例4

顯示編譯器版本的程式

-- importing Info module from System package
import System.Info
main = do
-- printing the version of the compiler
   print ("The version of the compiler is:")
   print(compilerVersion)

輸出

"The version of the compiler is:"
Version {versionBranch = [8,6], versionTags = []}

在上面的程式中,我們從System包中匯入了Info模組。在main函式中,我們呼叫了一個函式compilerVersion (compilerVersion :: [Char]),它是Info模組中返回用於編譯程式碼的編譯器版本的內建函式。最後,使用print函式列印返回的輸出。

結論

在本教程中,我們討論了在Haskell程式語言中實現查詢系統資訊的程式。

更新於:2022年12月14日

瀏覽量:172

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告