user3097712
user3097712

Reputation: 1675

Import a function in haskell

I´ve written a function in which i say which number is odd. And then in a second function, I want to say which number is odd from a given list. So my question is:

How can I include the data file for the first function, e.g. odd.hs, in the second data file, e.g. oddNbrs.hs. to use the first function in the second function ?

For example in C I have "include" as you know. Exists something like "include" in haskell, too? Or?

edit: Both files are of course in the same directory. Is that enough, when I mention the name first function in the second function, or should I do a different thing ?

Upvotes: 0

Views: 756

Answers (1)

epsilonhalbe
epsilonhalbe

Reputation: 15967

I think the keyword you are looking for is module.

file: Odd.hs

module Odd where

isOdd :: Int -> Bool
isOdd x = x `rem` 2 /= 0

file: Even.hs

module Even where

import Odd

isEven :: Int -> Bool
isEven = not isOdd

file: Numbers.hs

module Numbers where

import Odd
import Even

theNumbers :: [Int]
theNumbers = [1..100]

theOddNumbers  = filter isOdd theNumbers
theEvenNumbers = filter isEven theNumbers

Or, more elegantly in one module.

file: Predicates.hs

module Predicates where

isEven ...
isOdd

file: Main.hs

module Main where

import Predicates (isEven)

numbers = [1..100]

main :: IO()
main = mapM_ print $ filter isEven numbers

Upvotes: 5

Related Questions