計算機科学のブログ

コードの整理とプロジェクトのビルド Monad型クラス stackを使ってプロジェクトをビルドする コードを記述する パッケージの設定、モジュール、LANGUAGEプラグマ

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

README.md

# palindrome-checker

回文か判定

package.yaml

name:                palindrome-checker
version:             0.1.0.0
github:              "githubuser/palindrome-checker"
license:             BSD3
author:              "kamimura"
maintainer:          "kamimura@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

library:
  source-dirs: src
  default-extensions: OverloadedStrings

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

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

palindrome-checker.cabal

cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.34.4.
--
-- see: https://github.com/sol/hpack

name:           palindrome-checker
version:        0.1.0.0
description:    Please see the README on GitHub at <https://github.com/githubuser/palindrome-checker#readme>
homepage:       https://github.com/githubuser/palindrome-checker#readme
bug-reports:    https://github.com/githubuser/palindrome-checker/issues
author:         kamimura
maintainer:     kamimura@example.com
copyright:      2022 kamimura
license:        BSD3
license-file:   LICENSE
build-type:     Simple
extra-source-files:
    README.md
    ChangeLog.md

source-repository head
  type: git
  location: https://github.com/githubuser/palindrome-checker

library
  exposed-modules:
      Lib
      Palindrome
  other-modules:
      Paths_palindrome_checker
  hs-source-dirs:
      src
  default-extensions:
      OverloadedStrings
  build-depends:
      base >=4.7 && <5
    , text
  default-language: Haskell2010

executable palindrome-checker-exe
  main-is: Main.hs
  other-modules:
      Paths_palindrome_checker
  hs-source-dirs:
      app
  default-extensions:
      OverloadedStrings
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , palindrome-checker
    , text
  default-language: Haskell2010

test-suite palindrome-checker-test
  type: exitcode-stdio-1.0
  main-is: Spec.hs
  other-modules:
      Paths_palindrome_checker
  hs-source-dirs:
      test
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , palindrome-checker
    , text
  default-language: Haskell2010

コード

app/Main.hs

module Main where

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

main :: IO ()
main = do
  sumFunc
  TIO.putStrLn "Enter a word I'll let you know if it's a palindrome!"
  word <- TIO.getLine
  TIO.putStrLn $
    if isPalindrome word
      then "it is!"
      else "it's not!"

コード

src/Palindrome.hs

module Palindrome
  ( isPalindrome,
  )
where

import Data.Char (isPunctuation)
import qualified Data.Text as T

isPalindrome :: T.Text -> Bool
isPalindrome text =
  let clearText = preprocess text
   in clearText == T.reverse clearText

preprocess :: T.Text -> T.Text
preprocess = stripWhiteSpace . stripPunctuation . toLowerCase

stripWhiteSpace :: T.Text -> T.Text
stripWhiteSpace text = T.intercalate "" $ T.words text

stripPunctuation :: T.Text -> T.Text
stripPunctuation text =
  T.pack $
    filter
      (not . isPunctuation)
      $ T.unpack text

toLowerCase :: T.Text -> T.Text
toLowerCase = T.toLower

入出力結果(Terminal, Zsh)

% stack setup
stack will use a sandboxed GHC it installed
For more information on paths, see 'stack path' and 'stack exec env'
To use this GHC and packages outside of a project, consider using:
stack ghc, stack ghci, stack runghc, or stack exec
% stack build
palindrome-checker-0.1.0.0: unregistering (local file changes: app/Main.hs)
palindrome-checker> build (lib + exe)
Preprocessing library for palindrome-checker-0.1.0.0..
Building library for palindrome-checker-0.1.0.0..
Preprocessing executable 'palindrome-checker-exe' for palindrome-checker-0.1.0.0..
Building executable 'palindrome-checker-exe' for palindrome-checker-0.1.0.0..
[2 of 2] Compiling Main
Linking .stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/palindrome-checker-exe/palindrome-checker-exe ...
palindrome-checker> copy/register
Installing library in /Users/…/palindrome-checker/.stack-work/install/x86_64-osx/e39fa63852b3f10f810d09adf64598107c29e3897fb5f28ebd525048b485f8ab/8.10.7/lib/x86_64-osx-ghc-8.10.7/palindrome-checker-0.1.0.0-3R7ppxjAjvo6K9JkXgTIAf
Installing executable palindrome-checker-exe in /Users/…/palindrome-checker/.stack-work/install/x86_64-osx/e39fa63852b3f10f810d09adf64598107c29e3897fb5f28ebd525048b485f8ab/8.10.7/bin
Registering library for palindrome-checker-0.1.0.0..
% stack exec palindrome-checker-exe
someFunc
Enter a word I'll let you know if it's a palindrome!
AbCBa
it is!
% stack exec palindrome-checker-exe
someFunc
Enter a word I'll let you know if it's a palindrome!
Haskell
it's not!
% stack exec palindrome-checker-exe
someFunc
Enter a word I'll let you know if it's a palindrome!
しんぶんし
it is!
%