「 Haskel を Ubuntu にインストールできねーの?」
「 Windows と Ubuntu で Git Hub が違う」
「 Visual Studio Code の Ubuntu 版は無いの?」
「 ↓ うーん、そのうち 願いが叶えられそうなのは これかな?」
Input:
sudo snap install --classic code
Output:
code 1a5daa3a from Visual Studio Code (vscode✓) installed
「 ↑ インストールされたらしいけど、どうやって起動する?」
Input:
code
「 ↑ うおっ、CUI から GUI が出てきた メモ帳みたいだ」
「 コマンド・プロンプトと 似たようなもんなんじゃないか?」
Input:
# インストール
sudo apt-get install git
Input:
# インストールできたか確認
dpkg -l git
「 バージョンは 1:2.34.1-1ubuntu1.10
って書いてあるぜ」
# git の初期設定
git config --global user.name [任意のユーザ名]
git config --global user.email [任意のユーザ名]
「 メールアドレスは覚えてるだろ。 Git Hub を見ると お父んの名前は muzudho
だぜ」
Input:
git config --list
「 ↓ Web ブラウザーで Git Hub を開いて リポジトリーを作っておいたぜ」
Input:
cd ~
ll
cd Documents
ll
「 GUI で見ると Home/Documents
というところに いろいろ置くといいんじゃない?」
mkdir git_hub
cd git_hub
mkdir example1
cd example1
git init
「 おっ、 Home/Documents/git_hub/example1
フォルダーの中に .git フォルダーが作られたぜ」
「 ローカル・リポジトリを 新規作成するのではなくて、
Git Hub のリポジトリを クローンしたかったのでは?」
「 ↓ Git Hub で こんなコマンドライン コピーしてきたけど、使えるのかな?」
gh repo clone muzudho/haskell-practice-on-ubuntu
「 カレントディレクトリーを Home/Documents/git_hub
へ移動」
git clone https://github.com/muzudho/haskell-practice-on-ubuntu
「 Git Hub へ ファイルをアップロードする練習をしなさいよ?」
「 Home/Documents/git_hub/haskell-practice-on-ubuntu
へ、フォルダーやファイルを追加して」
Input:
cd haskell-practice-on-ubuntu
git add Docs
git commit
Input:
git push origin
「 ユーザー名と パスワードを入れろと 出てきたな、忘れたな」
「 ↓ なんだか分からないが Git Hub CLI をインストールしてみよう」
Installing gh on Linux and BSD
Input:
type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y
Input:
sudo apt update
Input:
sudo apt install gh
「 gh
をインストールしたところで push できないぜ」
gh auth login
「 ↑ 認証トークンを貼れ、とか出てくるが、なんのことだか」
「 逆だ、コマンドラインからウェブ・ブラウザを開いて こっちから 認証コードを打ち込みに行くんだ。認証できた」
Input:
git push
Output:
Everything up-to-date
「 git commit
、 nano エディターの使い方が分からんな。コメント入れて保存して」
#cd レポジトリのディレクトリ
git commit -a
「 ↑ git commit -a
で ローカルのリポジトリに ぶち込むらしい」
「 ↑ なんか コメントを打鍵して [Ctrl] + [O]
キーで COMMIT_EDITMSG
ファイルを保存、
[Ctrl] + [X]
キーでエディターを終了」
git push
「 ↑ git push
で リモートのリポジトリに ぶち上げるらしい」
「 ↑ Visual Studio Code に Haskell の拡張があるらしいんで、それを探してみるか」
📖 GHCup
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
「 ↓ イエスと答え続けていくと コマンドを打てと出てきた」
System requirements
Please ensure the following distro packages are installed before continuing (you can exit ghcup and return at any time): build-essential curl libffi-dev libffi8ubuntu1 libgmp-dev libgmp10 libncurses-dev libncurses5 libtinfo5
===============================================================================
OK! /home/muzudho/.bashrc has been modified. Restart your terminal for the changes to take effect,
or type "source /home/muzudho/.ghcup/env" to apply them in your current terminal session.
===============================================================================
All done!
To start a simple repl, run:
ghci
To start a new haskell project in the current directory, run:
cabal init --interactive
To install other GHC versions and tools, run:
ghcup tui
If you are new to Haskell, check out https://www.haskell.org/ghcup/steps/
📖 https://www.haskell.org/ghcup/steps/
ghc --version
Command 'ghc' not found, but can be installed with:
sudo apt install ghc
ghci --version
Command 'ghci' not found, but can be installed with:
sudo apt install ghc
ghci
Command 'ghci' not found, but can be installed with:
sudo apt install ghc
sudo apt install ghc
ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.8.4
📖 https://wiki.haskell.org/Haskell
「 ↑ 9.4.8 にアップデートするには どうすればいい?」
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
「 ↑ 分からんから もう1回コマンドを叩いて アペンドする」
ghci
GHCi, version 8.8.4: https://www.haskell.org/ghc/ :? for help
Prelude>
📖 https://www.haskell.org/ghc/blog/20231009-ghc-9.8.1-released.html
「 ↑ こいつを どうやってインストールするか 調べてみるか」
ghci
GHCi, version 8.8.4: https://www.haskell.org/ghc/ :? for help
Prelude> main = putStrLn "Hello, Haskell!"
Prelude>
Prelude> main
Hello, Haskell!
Prelude>
📖 https://www.haskell.org/ghcup/steps/
「 ハスケルのファイル名は hello.hs
みたいに名付けるそうよ」
🗒 hello.hs
:
main = putStrLn "Hello, Haskell!"
ghc hello.hs
「 ↑ コンパイルが必要なようだぜ。 hello
, hello.hi
, hello.o
ファイルが生成されたぜ」
./hello
Hello, Haskell!
「 ↑ hello
コマンドができたのかだぜ? あっ、動いた」
「 ghci
がインタープリター・モードになってんじゃないの? それで練習するんじゃないの?」
ghci
GHCi, version 8.8.4: https://www.haskell.org/ghc/ :? for help
Prelude> 1 + 1
2
Prelude> putStrLn "Hello, world!"
Hello, world!
Prelude> double x = x + x
Prelude> double 2
4
「 ↑ 数を2倍にする double 関数を作って 実行したようだぜ、飽きてきた」
Prelude> :{
Prelude| map f list =
Prelude| case list of
Prelude| [] -> []
Prelude| x : xs -> f x : map f xs
Prelude| :}
Prelude>
Prelude>
Prelude>
Prelude> :q
Leaving GHCi.
Prelude> :{
Prelude| map f list =
Prelude| case list of
Prelude| [] -> []
Prelude| x : xs -> f x : map f xs
Prelude| :}
Prelude> map
<interactive>:7:1: error:
• No instance for (Show ((t0 -> a0) -> [t0] -> [a0]))
arising from a use of ‘print’
(maybe you haven't applied a function to enough arguments?)
• In a stmt of an interactive GHCi command: print it
Prelude> map (+1) [1, 2, 3]
[2,3,4]
Prelude> :load hello.hs
[1 of 1] Compiling Main ( hello.hs, interpreted )
Ok, one module loaded.
*Main> main
Hello, Haskell!
*Main> import Data.Bits
*Main Data.Bits> shiftL 32 1
64
*Main Data.Bits> clearBit 33 0
32
:type putStrLn
putStrLn :: String -> IO ()
:quit
Leaving GHCi.
cabal repl --build-depends async,say
Command 'cabal' not found, but can be installed with:
sudo apt install cabal-install
stack exec --package async --package say -- ghci
Command 'stack' not found, but can be installed with:
sudo apt install haskell-stack
📖 https://www.haskell.org/ghcup/install/
📺 https://www.youtube.com/watch?v=bB4fmQiUYPw
「 ↑ 動画もあるが Windows で参考にならねー。飽きてきた」
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
[ Warn ] [GHCup-00110] ghc-9.4.7 is already installed; if you really want to reinstall it, you may want to run 'ghcup install ghc --force 9.4.7'
[ Info ] GHC 9.4.7 successfully set as default version
[ Warn ] [GHCup-00110] cabal-3.6.2.0-p1 is already installed; if you really want to reinstall it, you may want to run 'ghcup install cabal --force 3.6.2.0-p1'
「 ↑ cabel はインストールされてるらしいが、コマンドは無い」
ghcup install cabal --force 3.6.2.0-p1
ghcup: command not found
「 Visual Studio Code を再起動 してみては?」
ghcup --version
The GHCup Haskell installer, version 0.1.20.0
ghc --version
The Glorious Glasgow Haskell Compilation System, version 9.4.7
ghci --version
The Glorious Glasgow Haskell Compilation System, version 9.4.7
cabal repl --build-depends async,say
Resolving dependencies...
Build profile: -w ghc-9.4.7 -O1
In order, the following will be built (use -v for more details):
- hashable-1.4.3.0 (lib) (requires download & build)
- say-0.1.0.1 (lib) (requires download & build)
- async-2.2.5 (lib) (requires download & build)
- fake-package-0 (lib) (first run)
Downloading hashable-1.4.3.0
Downloaded hashable-1.4.3.0
Downloading async-2.2.5
Starting hashable-1.4.3.0 (lib)
Building hashable-1.4.3.0 (lib)
Downloaded async-2.2.5
Downloading say-0.1.0.1
Downloaded say-0.1.0.1
Starting say-0.1.0.1 (lib)
Building say-0.1.0.1 (lib)
Installing hashable-1.4.3.0 (lib)
Installing say-0.1.0.1 (lib)
Completed hashable-1.4.3.0 (lib)
Starting async-2.2.5 (lib)
Completed say-0.1.0.1 (lib)
Building async-2.2.5 (lib)
Installing async-2.2.5 (lib)
Completed async-2.2.5 (lib)
Configuring library for fake-package-0..
Preprocessing library for fake-package-0..
Warning: No exposed modules
GHCi, version 9.4.7: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /tmp/cabal-repl.-149129/setcwd.ghci
ghci>
ghci> import Control.Concurrent.Async
ghci> import Say
ghci> concurrently_ (sayString "Hello") (sayString "World")
Hello
World
:quit
Leaving GHCi.
cabal install --lib async say --package-env .
Warning: Unknown/unsupported 'ghc' version detected (Cabal 3.6.2.0 supports
'ghc' version < 9.4): /home/muzudho/.ghcup/bin/ghc is version 9.4.7
Warning: Unknown/unsupported 'ghc' version detected (Cabal 3.6.2.0 supports
'ghc' version < 9.4): /home/muzudho/.ghcup/bin/ghc is version 9.4.7
Resolving dependencies...
Up to date
mkdir haskell-project
cd haskell-project
cabal init --interactive
Should I generate a simple project with sensible defaults? [default: y]
Guessing dependencies...
Generating LICENSE...
Warning: unknown license type, you must put a copy in LICENSE yourself.
Generating CHANGELOG.md...
Generating src/MyLib.hs...
Generating app/Main.hs...
Generating haskell-project.cabal...
Warning: no synopsis given. You should edit the .cabal file and add one.
You may want to edit the .cabal file and add a Description field.
Input:
cabal build
Output:
Warning: Unknown/unsupported 'ghc' version detected (Cabal 3.6.2.0 supports
'ghc' version < 9.4): /home/muzudho/.ghcup/bin/ghc is version 9.4.7
Resolving dependencies...
Build profile: -w ghc-9.4.7 -O1
In order, the following will be built (use -v for more details):
- haskell-project-0.1.0.0 (lib) (first run)
- haskell-project-0.1.0.0 (exe:haskell-project) (first run)
Configuring library for haskell-project-0.1.0.0..
Preprocessing library for haskell-project-0.1.0.0..
Building library for haskell-project-0.1.0.0..
[1 of 1] Compiling MyLib ( src/MyLib.hs, /home/muzudho/Documents/git_hub/haskell-practice-on-ubuntu/haskell-project/dist-newstyle/build/x86_64-linux/ghc-9.4.7/haskell-project-0.1.0.0/build/MyLib.o, /home/muzudho/Documents/git_hub/haskell-practice-on-ubuntu/haskell-project/dist-newstyle/build/x86_64-linux/ghc-9.4.7/haskell-project-0.1.0.0/build/MyLib.dyn_o )
Configuring executable 'haskell-project' for haskell-project-0.1.0.0..
Preprocessing executable 'haskell-project' for haskell-project-0.1.0.0..
Building executable 'haskell-project' for haskell-project-0.1.0.0..
[1 of 1] Compiling Main ( app/Main.hs, /home/muzudho/Documents/git_hub/haskell-practice-on-ubuntu/haskell-project/dist-newstyle/build/x86_64-linux/ghc-9.4.7/haskell-project-0.1.0.0/x/haskell-project/build/haskell-project/haskell-project-tmp/Main.o )
[2 of 2] Linking /home/muzudho/Documents/git_hub/haskell-practice-on-ubuntu/haskell-project/dist-newstyle/build/x86_64-linux/ghc-9.4.7/haskell-project-0.1.0.0/x/haskell-project/build/haskell-project/haskell-project
Input:
cabal run
output:
Up to date
Hello, Haskell!
someFunc
「 haskell-project.cabal
ファイルを Visual Studio Code で開くぜ」
🗒 haskell-project.cabal
:
cabal-version: 2.4
name: haskell-project
version: 0.1.0.0
-- A short (one-line) description of the package.
-- synopsis:
-- A longer description of the package.
-- description:
-- A URL where users can report bugs.
-- bug-reports:
-- The license under which the package is released.
-- license:
author: muzudho
maintainer: [email protected]
-- A copyright notice.
-- copyright:
-- category:
extra-source-files: CHANGELOG.md
library
exposed-modules: MyLib
-- Modules included in this library but not exported.
-- other-modules:
-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends: base ^>=4.17.2.0
hs-source-dirs: src
default-language: Haskell2010
executable haskell-project
main-is: Main.hs
-- Modules included in this executable, other than Main.
-- other-modules:
-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends:
base ^>=4.17.2.0,
haskell-project
hs-source-dirs: app
default-language: Haskell2010
「 src/MyLib.hs
ファイルを Visual Studio Code で開くぜ」
🗒 src/MyLib.hs
:
module MyLib (someFunc) where
import System.Directory
someFunc :: IO ()
someFunc = do
contents <- listDirectory "src"
putStrLn (show contents)
🗒 src/OtherLib.hs
:
module OtherLib where
otherFunc :: String -> Int
otherFunc str = length str
cabal repl
ghci> import OtherLib
<no location info>: error:
Could not find module ‘OtherLib’
It is not a module in the current program, or in any known package.```
🗒 haskell-project.cabal
:
library
exposed-modules: MyLib
🗒 haskell-project.cabal
:
library
exposed-modules: MyLib
OtherLib
cabal repl
Resolving dependencies...
Build profile: -w ghc-9.4.7 -O1
In order, the following will be built (use -v for more details):
- haskell-project-0.1.0.0 (lib) (configuration changed)
Configuring library for haskell-project-0.1.0.0..
Preprocessing library for haskell-project-0.1.0.0..
GHCi, version 9.4.7: https://www.haskell.org/ghc/ :? for help
[1 of 2] Compiling MyLib ( src/MyLib.hs, interpreted )
[2 of 2] Compiling OtherLib ( src/OtherLib.hs, interpreted )
Ok, two modules loaded.
ghci> import OtherLib
ghci> otherFunc "Hello Haskell"
13
「 エグザンプルは ここで終わってるぜ。あとは勝手に調べろとのことだぜ」
fx(x) = x^2
プログラミングの関数は、この数学の関数ではない
プログラミングの関数は
ブロックを組み立てるようなもの
FP Concepts
Haskell Features
Haskell Toolchain
Haskell Toolchain / GHCi
()
are important!:t
や :tpye
で型を見れる。 型の型は見れない:t +d
で調べるTypes of functions -> :t div
より、 :t +d div
を使って調べる。2項の引数のとき、見やすい
List
[]
の話。 [] ++ []
とか!!
- この操作は処理が遅い。インデックスを使ったランダムアクセスか?[1 .. 10]
や、 [1, 3 .. 20]
[10 .. 1]
は空っぽ。 [10, 9 .. 1]
はできる[0 .. ]
インフィニティもできるLazy evaluation
take 3 [0 .. ]
で、 [0, 1, 2]
take 3 (drop 5 [0 .. ]
で、[5,6,7]
length [0 .. ]
- 終わらない。無限のリストには注意ディスプレイの信号が消えた
Syntax
Packages
if-then-else
def list = if null list then def else head list
headOfDefault def list =
if null list
then def
else head list
sign n
| 略
続き
let-in
- リストに含まれているか調べる?where
- 関数定義で使う?immutablirity
- 1回だけ代入できるというものHigher-order functions
:q
:t
:t +d
:l
:i
「 ↓ 基礎勉強は もういいだろ、コンピューター将棋を作ろうぜ?」
📖 Haskell でコンピューター将棋を作ろうぜ(^o^)?
📖 Haskell Beginners 2022: Lecture 2
case-of
- スイッチ文みたいなやつか?eval :: Char -> Int -> Int -> Int
eval op x y = case op of
'+' -> x + y
'-' -> x - y
'*' -> x * y
'/' -> div x y
_ -> 0
続き
What's wrong, Haskell?
- 書き方でコンパイル・エラーが出る例、エラーは出ないが悪い書き方の例Totality
- すべての入力に対して出力が定義されていることか?
Partial
- 非 totalData
Product Type
- 異なる型などを結合したもの
data: Product
data: Working with data
- セッターとゲッターの書き方data: Records
- 構造体みたいなもんSum Type
Sum Types: Enumerations
- いわゆる列挙型Sum Types
- Result とかRecursive data types
Empty list
An eleement prepended to a list
type
- type キーワード。型の別名? 式が見やすくなるnewtype
- コンストラクタの別名??Parametric polymorphism
- ジェネリクスみたいなやつReal types
Hoogle
- Haskell Search EnginePolymorphic types
Common types
- Optional Value とか、左右とかCommon Functions
Eta-reduction, part1
Eta-reduction, part2
Function Composition
ELI5 Composition
Composition + Eta-reduction
📖 Haskell Beginners 2022: Lecture 3
Parametric Polymorphism
Ad-hoc polymorphism
{-# LANGUAGE #-}
- ランゲージ・プラグマ。Ord
- Ordering 比較演算子
deriving
- 派生<>
- ダイヤモンド演算子Semigroup
Monoid
Kind
- a type of a typeFunctor laws
- 10種類
1: Identity
2: Composition
ステップ関数
等式推論
{-# LANGUAGE BangPatterns #-}
📖 Haskell Beginners 2022: Lecture 4
andThen
- Ordinary helper function1. Nothing
2. Recognize
3. Copy-paste
4. Abstract
5. Put in a library
return
も関数(:[])
- robot monkey operatorpure
- ハスケルでは return の代わりに pure をよく使うPurity
- 純粋。副作用がない関数。
1. Determinism
2. Easier reasoning
3. Simpler testing
4. Composability
5. Optimizations
6. Parallelism
getLine
unit type
- 空の丸括弧main
- メイン関数do 構文
- do-notation
let-in rule
Example with do
.cabal
app
フォルダーと、 src
フォルダーに分ける。 app は実行可能で、 src はライブラリーか?build-depends
Hackage
- ユーザー作成のパッケージが置いてる?Functional,Imperative
Imperative style
- 命令型スタイルFunctional style
- 関数型スタイル📖 Haskell でコンピューター将棋を作ろうぜ(^o^)?
.
Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?
コメント