Haskell - 実践Haskell - Haskellでの効率的でステートフルの配列 - UArrayとSTArray, クロスオーバー(交叉)
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT 7(実践Haskell)、LESSON 42(Haskellでの効率的でステートフルの配列)、42.6(練習問題)、Q42-1の解答を求めてみる。
軽量マークアップ言語
st-lesson/package.yaml
name: st-lesson
version: 0.1.0.0
github: "githubuser/st-lesson"
license: BSD-3-Clause
author: "Author name here"
maintainer: "example@example.com"
copyright: "2025 Author name here"
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/st-lesson#readme>
dependencies:
- base >= 4.7 && < 5
- array
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:
st-lesson-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- st-lesson
tests:
st-lesson-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- st-lesson
コード
st-lesson/app/Main.hs
module Main (main) where
-- import Lib
import Control.Monad
import Data.Array.ST
import Data.Array.Unboxed
crossOver :: (UArray Int Int, UArray Int Int) -> Int -> UArray Int Int
crossOver (xs, ys) n = runSTUArray $ do
stArray <- thaw xs
let end = (snd . bounds) ys
forM_ [n .. end] $ \i -> do
writeArray stArray i $ ys ! i
return stArray
aArray :: UArray Int Int
aArray = array (0, 4) []
bArray :: UArray Int Int
bArray = array (0, 4) $ zip [0 .. 4] $ repeat 1
main :: IO ()
main = do
print aArray
print bArray
print $ crossOver (aArray, bArray) 3
print aArray
print bArray
入出力結果(Terminal, Zsh)
% stack run
st-lesson-0.1.0.0: unregistering (local file changes: app/Main.hs)
st-lesson> build (lib + exe) with ghc-9.8.4
Preprocessing library for st-lesson-0.1.0.0..
Building library for st-lesson-0.1.0.0..
Preprocessing executable 'st-lesson-exe' for st-lesson-0.1.0.0..
Building executable 'st-lesson-exe' for st-lesson-0.1.0.0..
[1 of 2] Compiling Main [Source file changed]
[3 of 3] Linking .stack-work/dist/aarch64-osx/ghc-9.8.4/build/st-lesson-exe/st-lesson-exe [Objects changed]
ld: warning: -U option is redundant when using -undefined dynamic_lookup
st-lesson> copy/register
Installing library in /Users/.../st-lesson/.stack-work/install/aarch64-osx/27e00dd04a6f7fc061662d4eebb16cc69859af4840b4ab18e626ce23aacafb60/9.8.4/lib/aarch64-osx-ghc-9.8.4/st-lesson-0.1.0.0-3lZxzIFyVubJ5mxkniV0U9
Installing executable st-lesson-exe in /Users/.../st-lesson/.stack-work/install/aarch64-osx/27e00dd04a6f7fc061662d4eebb16cc69859af4840b4ab18e626ce23aacafb60/9.8.4/bin
Registering library for st-lesson-0.1.0.0..
array (0,4) [(0,0),(1,0),(2,0),(3,0),(4,0)]
array (0,4) [(0,1),(1,1),(2,1),(3,1),(4,1)]
array (0,4) [(0,0),(1,0),(2,0),(3,1),(4,1)]
array (0,4) [(0,0),(1,0),(2,0),(3,0),(4,0)]
array (0,4) [(0,1),(1,1),(2,1),(3,1),(4,1)]
%