ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Haskellコミュの雑談・告知・簡単な質問など

  • mixiチェック
  • このエントリーをはてなブックマークに追加

コメント(60)

>>17
後から考えたらソースを見なくても Hoogle
http://www.haskell.org/hoogle/
で「GenParser」とか「Parser」とかを検索すればわかることでした。
以下のコードをコンパイルすると
mainがないと怒られます
どうすればexeになりますか? 
primes =[p | (p:_) <- iterate sieve [2..]]
sieve(p:ps) = [x| x<-ps, mod x p /=0]
>>23

main = print $ primes

という一文を加えてください。
おおっ 凄い!!
素数が無限に表示されます
ご教示有難うございます
以下のコーディングでコンパイルエラー
が出ます
どうなおしたらいいでしょうか?
main = print $ qsort
qsort::[int]->[int]
qsort [] = []
qsort(p:xs)=qsort lt ++ [p] ++ qsort gteq
where
lt = [x | x<-xs,x<p]
gteq = [x | x<-xs,x>=p]
・型名の頭は大文字でなければなりません
・インデントが構文として重要です
 (↓の全角スペースを半角スペースに直すとコンパイルできます)
・qsortはInt型のリストを引数として取る関数です

main = print $ qsort [4,2,6,1]
qsort::[Int]->[Int]
qsort [] = []
qsort(p:xs)=qsort lt ++ [p] ++ qsort gteq
  where
   lt = [x | x<-xs,x<p]
   gteq = [x | x<-xs,x>=p]
有難うございます
通りました
リスト内包表記を極めたいと思っています
すみません 以下のコードで
main parse errorになってしまいます
どうすればいいですか?

data BST a = Eot
| Node Int a (BST a) (BST a) -- key, val, left, right
deriving Eq

instance Show a => Show (BST a) where
show Eot = "Null"
show node = "BST" ++ (show $ bst2alist node)

-- find a record in a BST
bst_find :: BST a -> Int -> Maybe (Int, a)
bst_find Eot _ = Nothing
bst_find (Node key0 val0 left right) key
| key0 == key = Just (key0, val0)
| key0 < key = bst_find right key
| otherwise = bst_find left key

-- insert a record into a BST
bst_insert :: BST a -> (Int, a) -> BST a
bst_insert Eot (key, val) = Node key val Eot Eot
bst_insert (Node key0 val0 left right) (key, val)
| key0 == key = Node key0 val left right -- if same key value, replace val
| key0 < key = Node key0 val0 left (bst_insert right (key, val))
| otherwise = Node key0 val0 (bst_insert left (key, val)) right

-- traverse
bst_map :: (a -> b) -> BST a -> [(Int,b)]
bst_map _ Eot = []
bst_map f (Node key val left right) = bst_map f left ++ (key, f val) : bst_map f right

-- convert to an association list
bst2alist :: BST a -> [(Int, a)]
bst2alist = bst_map id

-- test data
bst1 = foldl bst_insert Eot [(5, "five"), (6,"six"), (4,"four"),
(3, "three"), (7, "seven"), (1,"One"), (10, "ten") ]

main = print $ bst_find ( 7, 6, "seven", "three") 7
下はmainの一例です。
bst_findの第一引数はBST a型ですから、(Int, Int, String, String)型を渡してはいけません。

main = print $ bst_find (Node 7 "6" (Node 6 "seven" Eot Eot) (Node 8 "three" Eot Eot)) 7
ありがとうございます
次ですが以下のコードで
アウトプットが

Just "Roger"

となるのですがコードを読んでも
どうしてそうなるのか分かりません
すみませんが解説お願いします

{- DESCRIPTION

Example 1 - Our first monad

Usage: Compile the code and execute the resulting program.
It will print Dolly's maternal grandfather.
-}

-- everything you need to know about sheep
data Sheep = Sheep {name::String, mother::Maybe Sheep, father::Maybe Sheep}

-- we show sheep by name
instance Show Sheep where
show s = show (name s)

-- comb is a combinator for sequencing operations that return Maybe
comb :: Maybe a -> (a -> Maybe b) -> Maybe b
comb Nothing _ = Nothing
comb (Just x) f = f x

-- now we can use `comb` to build complicated sequences
maternalGrandfather :: Sheep -> Maybe Sheep
maternalGrandfather s = (Just s) `comb` mother `comb` father

fathersMaternalGrandmother :: Sheep -> Maybe Sheep
fathersMaternalGrandmother s = (Just s) `comb` father `comb` mother `comb` mother

mothersPaternalGrandfather :: Sheep -> Maybe Sheep
mothersPaternalGrandfather s = (Just s) `comb` mother `comb` father `comb` father

-- this builds our sheep family tree
breedSheep :: Sheep
breedSheep = let adam = Sheep "Adam" Nothing Nothing
eve = Sheep "Eve" Nothing Nothing
uranus = Sheep "Uranus" Nothing Nothing
gaea = Sheep "Gaea" Nothing Nothing
kronos = Sheep "Kronos" (Just gaea) (Just uranus)
holly = Sheep "Holly" (Just eve) (Just adam)
roger = Sheep "Roger" (Just eve) (Just kronos)
molly = Sheep "Molly" (Just holly) (Just roger)
in Sheep "Dolly" (Just molly) Nothing

-- print Dolly's maternal grandfather
main :: IO ()
main = let dolly = breedSheep
in do print (maternalGrandfather dolly)

-- END OF FILE
Mollyの fatherがroger なので
"Dolly" (Just molly) Nothing
が
Sheep "Molly" (Just holly) (Just roger)
にマッチして 
Just "Roger"
になるのかな?
それにてもポイントフリースタイルって
何がいいのかわからん


順に追っていけば

maternalGrandfather dolly
=> maternalGrandfather breedSheep
=> (Just breedSheep) `comb` mother `comb` father
=> (mother breedSheep) `comb` father
=> (mother (Sheep "Dolly" (Just molly) Nothing)) `comb` father
=> (Just molly) `comb` father
=> father molly
=> father (Sheep "Molly" (Just holly) (Just roger))
=> Just roger
続きがあった。
print のときは show するから

show (Just roger)
=> Just (show roger)
=> Just (show (name roger))
=> Just (show "Roger")
=> Just "Roger"
毎日Haskellにあった以下のコード
parse error でコンパイル通りません
なんでかな?

import List
takeLast::Int->[Int]->[Int]
takeLast n xs = concat $ dropWhile (not.null.drop 1) $ transpose [xs, drop n xs]

main = print $ takeLast 2 [1..100]

>ghc takelast.hs -o takelast
で
takelast.hs:2:1: parse error on input 'takelast'
と出ます
2行目の takeLast の前にスペースが入っているとかいうことはありませんか?
>2行目の takeLast の前にスペースが入っているとかいうことは>ありませんか?
おぉ 確かに入ってました、取ったらコンパイル→実行できました
でもなんでこのスペースいけないんですか?
今までレイアウトの仕組みをちゃんと読んでいなかったので
「Haskell 98 言語とライブラリ 改訂レポート 2 字句構造」
http://www.sampou.org/haskell/report-revised-j/lexemes.html
の「2.7 レイアウト」を見てみました。

今回のケースでは2行目が1行目に継続された
import List takeLast::Int->[Int]->[Int]
のように解釈され parse error になるようです。
たったこれだけのコーディングで
経路探索ができる


-- easy search
-- module EasySearch where
import Monad
type Graph = ([Int], [(Int, Int)])

-- graph with loop
gr0::Graph
gr0 = ( [1,2,3,4,5,6,7,8,9],[(1,2),(1,3),(1,4),(2,5),(3,5),(3,6),(4,7),(5,1),(6,7),(6,8),(7,9),(8,4)])

find_next::Int -> Graph -> [Int]
find_next k gr = map snd $ filter ((==k) . fst) (snd gr)


-- depth first search
-- dfs :: Graph -> Int -> Int -> Maybe [Int]
dfs::Graph -> Int -> Int -> [[Int]]
dfs gr p0 p1 = dfs_aux gr p0 p1 []

dfs_aux gr p0 p1 path
| p0 == p1 = return (reverse (p1:path))
| otherwise = msum $
map (\p -> dfs_aux gr p p1 (p0:path))
[ x | x <- find_next p0 gr, not (x `elem` path)]
main = print $ dfs gr0 1 9
以下のコーディングで
parse error
が出てしまい 悩んでいます

calc::[Int]->[Int]
Z = [1..]
[x^2 + x + 2 | x <- Z, x > 20]
main = print $ calc
calc を定義するには「calc xs = 」みたいなかんじで書き始めないと定義できないですよ。
そこらへん書いてどうなるかみてみたらどうでしょう。
いろいろ試してトライアンドエラーを繰り返すと身につきますよ。
メモ書きということなので
メモを。

以下GHCのライブラリのドキュメント
http://www.haskell.org/ghc/docs/latest/html/libraries/array/Data-Array-IArray.html
に載っている一文ですが、何度コンパイルしてもエラーが起きます。

>a = array (1,100) ((1,1) : [(i, i * a!(i-1)) | i \<- [2..100]])

こちらはhaskell98-reportから引っ張ってきた同じ内容を表す部分です。
>a = array (1,100) ((1,1) : [(i, i * a!(i-1)) | i <- [2..100]])
こちらはエラーが起きません。


記述ミスみたいです。
日本語のHaskellの掲示板探したのですが見つからなかったので
ここで質問をさせてもらいます。

今Haskellに慣れるために色々なソートの実装をしています。
バブルソート、挿入ソート、クイックソートまでは実装できたのですが、
コームソートの実装方法が分かりません。

ヒントを頂けたら嬉しいです。
以下Newton法のコードなんですが
いまいち良く分かりません
特に??あたりがキモのようですが...
どなたか解説よろしくおねがいします

module Main (main) where

sqr :: Double -> Double
sqr c =
? let x = head $ dropWhile (\ x -> x * x < c) [1.0, 2.0 ..]
? in foldr (\ _ x -> (x + c / x) / 2.0) x [1 .. 10]

main :: IO ()
main = do
print $ sqr 2.0
print $ sqr 3.0
print $ sqr 4.0
calc を直してみました

calc::Int->Int
calc x = x^2 + x + 2
main::IO()
main = do
print $ [calc x | x <- [1,2,3,4,5]]


無限リストでコンピュータをイジメてみました
tri:: Double->Double
tri x = x^10000

main::IO()
main = do
print $ [tri x | x <- [1000000000000,40000000000000000 ..]]
haskell で grep を本格的(いろんなオプションもという意味で)に実装したオープンソースのものはありませんか。
実装中のものなら……ほとんどのオプションが not implemented の状態ですが。

http://hackage.haskell.org/cgi-bin/hackage-scripts/package/only
情報ありがとうございます。
ダウンロードしました。
GHC 6.10.x では、まだ共有ライブラリの作成に正式には対応していません。なので現状では、リンカに共有ライブラリを作らせるよう指定してやる必要があるようですね。

もう直ぐ RC が出る GHC 6.12.1 では共有ライブラリ作成の正式対応するので、GHC 6.12.1 以降では -shared オプションで直接共有ライブラリを作れるようになるはずです。

http://hackage.haskell.org/trac/ghc/wiki/Status/Releases
http://hackage.haskell.org/trac/ghc/ticket/1876#comment:30
 すみません。昨日、プログラミングHaskellを注文した、これから学ぼうとしているもの(初心者以前)ですが、Haskellの形式的意味論の定義はどこにあるのでしょうか?Googleで検索した程度では見つからないもので、、、、、ここで質問してよいでしょうか?
英語で検索...してみたけど完全なformal sematicsて提示されたことがないのかな?
質問なんですが、

"tes"という文字列リストの最後に't'を連結して"test"としたいんですが、

main=print$"tes":'t'
main=print$"tes"++'t'
main=print$"tes"++show 't'

どれを試しても、エラーが出たり、求めてるようにならなくて、うまくいきません。

方法を知ってる方がいらっしゃいましたら、お願いします。
この例だけだと素直に "tes" ++ "t" とすればいいわけですけど、
要は文字を文字列にする方法がわからないということですよね。
より一般化していうと、 a -> [a] がわかればいいということになります。

ちょうど a -> [a] になる関数は Prelude にはなくて、自分で (\x -> [x]) とでも書くしか無いのですが、
さらにもう一歩一般化して Monad m => a -> m a だと考えれば return になります。

なので
"tes" ++ return 't'
で達成できます。
>>57
回答ありがとうございます。

>要は文字を文字列にする方法がわからないということですよね。
そういうことです。

うまくいきました!しかし、なぜうまくいくのかわからないです…。

>さらにもう一歩一般化して Monad m => a -> m a だと考えれば return になります。
このあたりがまだまだ理解不足のようです。本を読んで理解できるようになりたいと思います。
ありがとうございました。

>>58
回答ありがとうございます!

なるほど!
そういうやり方でやるということもできるんですね…。まだまだ頭が固くて思いつきませんでした。
ありがとうございました。

ログインすると、残り25件のコメントが見れるよ

mixiユーザー
ログインしてコメントしよう!

Haskell 更新情報

Haskellのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。

人気コミュニティランキング