計算機科学のブログ

コードの整理とプロジェクトのビルド stackを使ってプロジェクトをビルドする コードを記述する

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

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
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
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

{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import qualified Data.Text.IO as TIO
import Palindrome

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"

lesson/src/Palindrome.hs

{-# LANGUAGE OverloadedStrings #-}

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
Enter a word and I'll let you know if it's a palindrome!
a
it is!
% stack exec palindrome-checker-exe
Enter a word and I'll let you know if it's a palindrome!
aba
it is!
% stack exec palindrome-checker-exe
Enter a word and I'll let you know if it's a palindrome!
ab
it's not
%