在haskell中导入函数
我写了一个函数,其中我说哪个数字是奇数。然后在第二个函数中,我想说给定列表中哪个数字是奇数。 所以我的问题是: 如何将第一个函数(例如odd.hs)的数据文件包含在第二个数据文件(例如oddnbr.hs)中。在第二个函数中使用第一个函数 例如,在C语言中,如您所知,我有“include”。haskell中也存在类似于“include”的东西吗?还是在haskell中导入函数,haskell,Haskell,我写了一个函数,其中我说哪个数字是奇数。然后在第二个函数中,我想说给定列表中哪个数字是奇数。 所以我的问题是: 如何将第一个函数(例如odd.hs)的数据文件包含在第二个数据文件(例如oddnbr.hs)中。在第二个函数中使用第一个函数 例如,在C语言中,如您所知,我有“include”。haskell中也存在类似于“include”的东西吗?还是 编辑:两个文件当然在同一个目录中。当我在第二个函数中提到第一个函数的名称时,这就足够了吗?或者我应该做另一件事吗 我想你要找的关键词是module
编辑:两个文件当然在同一个目录中。当我在第二个函数中提到第一个函数的名称时,这就足够了吗?或者我应该做另一件事吗 我想你要找的关键词是
module
文件:Odd.hs
module Odd where
isOdd :: Int -> Bool
isOdd x = x `rem` 2 /= 0
module Even where
import Odd
isEven :: Int -> Bool
isEven = not isOdd
module Numbers where
import Odd
import Even
theNumbers :: [Int]
theNumbers = [1..100]
theOddNumbers = filter isOdd theNumbers
theEvenNumbers = filter isEven theNumbers
module Predicates where
isEven ...
isOdd
module Main where
import Predicates (isEven)
numbers = [1..100]
main :: IO()
main = mapM_ print $ filter isEven numbers
文件:偶数.hs
module Odd where
isOdd :: Int -> Bool
isOdd x = x `rem` 2 /= 0
module Even where
import Odd
isEven :: Int -> Bool
isEven = not isOdd
module Numbers where
import Odd
import Even
theNumbers :: [Int]
theNumbers = [1..100]
theOddNumbers = filter isOdd theNumbers
theEvenNumbers = filter isEven theNumbers
module Predicates where
isEven ...
isOdd
module Main where
import Predicates (isEven)
numbers = [1..100]
main :: IO()
main = mapM_ print $ filter isEven numbers
文件:number.hs
module Odd where
isOdd :: Int -> Bool
isOdd x = x `rem` 2 /= 0
module Even where
import Odd
isEven :: Int -> Bool
isEven = not isOdd
module Numbers where
import Odd
import Even
theNumbers :: [Int]
theNumbers = [1..100]
theOddNumbers = filter isOdd theNumbers
theEvenNumbers = filter isEven theNumbers
module Predicates where
isEven ...
isOdd
module Main where
import Predicates (isEven)
numbers = [1..100]
main :: IO()
main = mapM_ print $ filter isEven numbers
编辑
在一个模块中更优雅
文件:Predicates.hs
module Odd where
isOdd :: Int -> Bool
isOdd x = x `rem` 2 /= 0
module Even where
import Odd
isEven :: Int -> Bool
isEven = not isOdd
module Numbers where
import Odd
import Even
theNumbers :: [Int]
theNumbers = [1..100]
theOddNumbers = filter isOdd theNumbers
theEvenNumbers = filter isEven theNumbers
module Predicates where
isEven ...
isOdd
module Main where
import Predicates (isEven)
numbers = [1..100]
main :: IO()
main = mapM_ print $ filter isEven numbers
文件:Main.hs
module Odd where
isOdd :: Int -> Bool
isOdd x = x `rem` 2 /= 0
module Even where
import Odd
isEven :: Int -> Bool
isEven = not isOdd
module Numbers where
import Odd
import Even
theNumbers :: [Int]
theNumbers = [1..100]
theOddNumbers = filter isOdd theNumbers
theEvenNumbers = filter isEven theNumbers
module Predicates where
isEven ...
isOdd
module Main where
import Predicates (isEven)
numbers = [1..100]
main :: IO()
main = mapM_ print $ filter isEven numbers
我想你要找的关键词是
module
文件:Odd.hs
module Odd where
isOdd :: Int -> Bool
isOdd x = x `rem` 2 /= 0
module Even where
import Odd
isEven :: Int -> Bool
isEven = not isOdd
module Numbers where
import Odd
import Even
theNumbers :: [Int]
theNumbers = [1..100]
theOddNumbers = filter isOdd theNumbers
theEvenNumbers = filter isEven theNumbers
module Predicates where
isEven ...
isOdd
module Main where
import Predicates (isEven)
numbers = [1..100]
main :: IO()
main = mapM_ print $ filter isEven numbers
文件:偶数.hs
module Odd where
isOdd :: Int -> Bool
isOdd x = x `rem` 2 /= 0
module Even where
import Odd
isEven :: Int -> Bool
isEven = not isOdd
module Numbers where
import Odd
import Even
theNumbers :: [Int]
theNumbers = [1..100]
theOddNumbers = filter isOdd theNumbers
theEvenNumbers = filter isEven theNumbers
module Predicates where
isEven ...
isOdd
module Main where
import Predicates (isEven)
numbers = [1..100]
main :: IO()
main = mapM_ print $ filter isEven numbers
文件:number.hs
module Odd where
isOdd :: Int -> Bool
isOdd x = x `rem` 2 /= 0
module Even where
import Odd
isEven :: Int -> Bool
isEven = not isOdd
module Numbers where
import Odd
import Even
theNumbers :: [Int]
theNumbers = [1..100]
theOddNumbers = filter isOdd theNumbers
theEvenNumbers = filter isEven theNumbers
module Predicates where
isEven ...
isOdd
module Main where
import Predicates (isEven)
numbers = [1..100]
main :: IO()
main = mapM_ print $ filter isEven numbers
编辑
在一个模块中更优雅
文件:Predicates.hs
module Odd where
isOdd :: Int -> Bool
isOdd x = x `rem` 2 /= 0
module Even where
import Odd
isEven :: Int -> Bool
isEven = not isOdd
module Numbers where
import Odd
import Even
theNumbers :: [Int]
theNumbers = [1..100]
theOddNumbers = filter isOdd theNumbers
theEvenNumbers = filter isEven theNumbers
module Predicates where
isEven ...
isOdd
module Main where
import Predicates (isEven)
numbers = [1..100]
main :: IO()
main = mapM_ print $ filter isEven numbers
文件:Main.hs
module Odd where
isOdd :: Int -> Bool
isOdd x = x `rem` 2 /= 0
module Even where
import Odd
isEven :: Int -> Bool
isEven = not isOdd
module Numbers where
import Odd
import Even
theNumbers :: [Int]
theNumbers = [1..100]
theOddNumbers = filter isOdd theNumbers
theEvenNumbers = filter isEven theNumbers
module Predicates where
isEven ...
isOdd
module Main where
import Predicates (isEven)
numbers = [1..100]
main :: IO()
main = mapM_ print $ filter isEven numbers
请考虑给出代码示例,当您询问问题时,请考虑给出代码示例,当您询问问题时,存在CPP(=C预处理器)和FFI(=外部函数接口),您可能会发现,当你在寻找
包括和haskell
-我认为这与你的问题无关!嗨,首先,谢谢你的回答。也可以在一个文件中同时包含这两个函数?这有可能吗?如果你有错误——如果你提供错误通常会有所帮助——否则你必须猜测你的问题是什么,这既乏味又令人不安。而且精神紧张的人回答问题的可能性较小;-)@user3097712注意,模块上有一些命名限制。它们的名称必须以大写字母开头,并且相应的文件必须命名为该名称+.hs
(除非您使用的是Literate Haskell,但这有点超出了这个问题的范围)。尤其是,您不能将它们的名称与函数的名称完全相同,因为函数名称必须以小写字母开头。唯一的例外是主模块文件,它不需要命名相同,因为它不是从其他地方导入的。此外,还存在CPP(=C-预处理器)和FFI(=外部函数接口),您可能会发现,当你在寻找包括和haskell
-我认为这与你的问题无关!嗨,首先,谢谢你的回答。也可以在一个文件中同时包含这两个函数?这有可能吗?如果你有错误——如果你提供错误通常会有所帮助——否则你必须猜测你的问题是什么,这既乏味又令人不安。而且精神紧张的人回答问题的可能性较小;-)@user3097712注意,模块上有一些命名限制。它们的名称必须以大写字母开头,并且相应的文件必须命名为该名称+.hs
(除非您使用的是Literate Haskell,但这有点超出了这个问题的范围)。尤其是,您不能将它们的名称与函数的名称完全相同,因为函数名称必须以小写字母开头。唯一的例外是主模块文件,它不需要命名相同,因为它不是从其他地方导入的。