tag:blogger.com,1999:blog-3636872937372639901.post5168089438818357738..comments2024-03-29T16:37:25.385+09:00Comments on 本の虫: Haskellでwordsを実装してみた江添亮http://www.blogger.com/profile/13387122818743087721noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-3636872937372639901.post-13814158947341704052021-08-08T19:33:39.430+09:002021-08-08T19:33:39.430+09:00Thank you so much for sharing this great blog.Very...<br /><br />Thank you so much for sharing this great blog.Very inspiring and<br /> helpful too.Hope you continue to share more of your ideas.<br />I will definitely love to read.<br /><br />Here's my website : -- <a href="https://op-best.com" rel="nofollow">안마</a><br><br />(freaky)opbesthttps://www.blogger.com/profile/01799304286846889739noreply@blogger.comtag:blogger.com,1999:blog-3636872937372639901.post-56779651046503825592017-12-26T16:24:09.701+09:002017-12-26T16:24:09.701+09:00以下のようにdropWhile'の型を明示すれば意図するエラーが出るはずです。
型推論にも限...以下のようにdropWhile'の型を明示すれば意図するエラーが出るはずです。<br />型推論にも限界はあるので、関数を定義するときは意識的に型を書きます。<br /><br />```<br />dropWhile' :: (t -> Bool) -> [t] -> [t] -- 型の明示<br />dropWhile' _ [] = []<br />dropWhile' f p@(x:xs) =<br /> if f x<br /> then dropWhile' xs -- 確認のため間違っておく<br /> else p<br />```<br /><br />----- 型を指定しない場合 -----<br />dropWhile.hs:1:1: error:<br /> • Couldn't match type ‘a -> Bool’ with ‘[a]’<br /> Expected type: [a] -> [a]<br /> Actual type: (a -> Bool) -> [a] -> [a]<br /> • Relevant bindings include<br /> dropWhile' :: [a] -> [a] (bound at dropWhile.hs:1:1)<br /> |<br />1 | dropWhile' _ [] = []<br /> | ^^^^^^^^^^^^^^^^^^^^...<br />-------------------------<br /><br />----- 型を指定した場合 -----<br />dropWhile.hs:5:14: error:<br /> • Couldn't match expected type ‘[t]’<br /> with actual type ‘[t0] -> [t0]’<br /> • Probable cause: ‘dropWhile'’ is applied to too few arguments<br /> In the expression: dropWhile' xs<br /> In the expression: if f x then dropWhile' xs else p<br /> In an equation for ‘dropWhile'’:<br /> dropWhile' f p@(x : xs) = if f x then dropWhile' xs else p<br /> • Relevant bindings include<br /> xs :: [t] (bound at dropWhile.hs:3:19)<br /> x :: t (bound at dropWhile.hs:3:17)<br /> p :: [t] (bound at dropWhile.hs:3:14)<br /> f :: t -> Bool (bound at dropWhile.hs:3:12)<br /> dropWhile' :: (t -> Bool) -> [t] -> [t] (bound at dropWhile.hs:2:1)<br /> |<br />5 | then dropWhile' xs<br /> | ^^^^^^^^^^^^^<br /><br />dropWhile.hs:5:25: error:<br /> • Couldn't match expected type ‘t0 -> Bool’ with actual type ‘[t]’<br /> • In the first argument of ‘dropWhile'’, namely ‘xs’<br /> In the expression: dropWhile' xs<br /> In the expression: if f x then dropWhile' xs else p<br /> • Relevant bindings include<br /> xs :: [t] (bound at dropWhile.hs:3:19)<br /> x :: t (bound at dropWhile.hs:3:17)<br /> p :: [t] (bound at dropWhile.hs:3:14)<br /> f :: t -> Bool (bound at dropWhile.hs:3:12)<br /> dropWhile' :: (t -> Bool) -> [t] -> [t] (bound at dropWhile.hs:2:1)<br /> |<br />5 | then dropWhile' xs<br /> | ^^<br />------------------------Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3636872937372639901.post-53309264827791831502017-12-26T15:27:27.655+09:002017-12-26T15:27:27.655+09:00ghc-8.2からエラーメッセージが親切になっているのでそちら使ってください。
lts-10.0から...ghc-8.2からエラーメッセージが親切になっているのでそちら使ってください。<br />lts-10.0からghc-8.2です。Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3636872937372639901.post-38862919848267762152017-12-26T15:05:15.756+09:002017-12-26T15:05:15.756+09:00あと、関数型のデバッグするならインタプリタに関数の一部分だけ取り出して引数を与えREPLするのがはや...あと、関数型のデバッグするならインタプリタに関数の一部分だけ取り出して引数を与えREPLするのがはやい<br />手続き型と違ってHaskellは参照透過性があるのが売りなので関数に与えている値一つ一つを取り出してどう動作しているかテストすることができるのでprintfデバッグよりどこでエラーが出たのか問題を分割して実行していくのが早い REPLとエディタを並べておくと便利Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3636872937372639901.post-17223356538953361302017-12-26T14:58:08.696+09:002017-12-26T14:58:08.696+09:00入出力だけならIOでハンドルつかって手続きを書くだけ
ただ問題は、Haskellらしくやるにはさらに...入出力だけならIOでハンドルつかって手続きを書くだけ<br />ただ問題は、Haskellらしくやるにはさらにpipesやconduitなどのストリーム処理ライブラリを覚える必要があることだ<br />これらは、(言語内で標準入出力を利用するかの如く)ストリームからの入出力操作だけを行えるモナディックなインターフェイスを提供する。これを用いることで例えば入力が標準入力だろうがプログラム中で行われたファイル読み込みだろうがリストだろうがポリモーフィズムに扱うことができる<br /><br />わざわざ複数種類があるのはインターフェイスや実装の思想的な違いや機能性、速度で差があるからだな・・・ここら辺Haskellはまだまだ思想的にもライブラリ実装的にも発展途中という印象Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3636872937372639901.post-64015497391085522252017-12-26T08:44:43.173+09:002017-12-26T08:44:43.173+09:00定義が間違っていた場合呼び出しが間違っていると指摘されるのは混乱の元だ定義が間違っていた場合呼び出しが間違っていると指摘されるのは混乱の元だAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-3636872937372639901.post-68090753014949793192017-12-26T08:18:39.490+09:002017-12-26T08:18:39.490+09:00ちなみに、本当の意味で入出力処理を扱えるようになるのはconduitライブラリを覚えた後だちなみに、本当の意味で入出力処理を扱えるようになるのはconduitライブラリを覚えた後だAnonymousnoreply@blogger.com