計算機科学のブログ

コードの整理とプロジェクトのビルド stackを使ってプロジェクトをビルドする 簡単な改善:LANGUAGEプラグマについて

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT6(コードの整理とプロジェクトのビルド)、LESSON 35(stackを使ってプロジェクトをビルドする)、35.6(練習問題)Q35-1の解答を求めてみる。

palindrome-checker/package.yaml

name:                palindrome-checker
version:             0.1.0.0
github:              "githubuser/palindrome-checker"
license:             BSD3
author:              "kamimura"
maintainer:          "example@example.com"
copyright:           "2022 kamimura"

extra-source-files:
- README.md
- CHANGELOG.md

# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at <https://github.com/githubuser/palindrome-checker#readme>

dependencies:
- base >= 4.7 && < 5
- text

default-extensions:
- OverloadedStrings

ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints

library:
  source-dirs: src

executables:
  palindrome-checker-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - palindrome-checker

tests:
  palindrome-checker-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - palindrome-checker

palindrome-checker/palindrome-checker.cabal

name:                palindrome-checker
version:             0.1.0.0
github:              "githubuser/palindrome-checker"
license:             BSD3
author:              "kamimura"
maintainer:          "example@example.com"
copyright:           "2022 kamimura"

extra-source-files:
- README.md
- CHANGELOG.md

# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at <https://github.com/githubuser/palindrome-checker#readme>

dependencies:
- base >= 4.7 && < 5
- text

default-extensions:
- OverloadedStrings

ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints

library:
  source-dirs: src

executables:
  palindrome-checker-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - palindrome-checker

tests:
  palindrome-checker-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - palindrome-checker

コード

lesson/app/Main.hs

module Main (main) where

import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import Palindrome (isPalindrome)
import Lib (sumFunc)

p :: T.Text -> IO ()
p text =
  TIO.putStrLn $
    if isPalindrome text
      then "it is!"
      else "it's not"

main :: IO ()
main = do
  -- TIO.putStrLn "Enter a word and I'll let you know if it's a palindrome!"
  -- text <- TIO.getLine
  -- TIO.putStrLn $
  --   if isPalindrome text
  --     then "it is!"
  --     else "it's not"
  mapM_ p ["", "a", "ab", "aba"]
  sumFunc

lesson/src/Lib.hs

module Lib
  ( sumFunc,
  )
where

sumFunc :: IO ()
sumFunc = putStrLn "someFunc"

lesson/src/Palindrome.hs

module Palindrome
  ( isPalindrome,
  )
where

import qualified Data.Text as T

isPalindrome :: T.Text -> Bool
isPalindrome text = text == T.reverse text

入出力結果(Terminal, Zsh)

% stack exec palindrome-checker-exe
it is!
it is!
it's not
it is!
someFunc
%