Skip to content

Instantly share code, notes, and snippets.

main = putStrLn "Hello"
@nushio3
nushio3 / Dice.hs
Created January 5, 2012 07:11
サイコロを振るモナド ref: http://qiita.com/items/1579
{-# OPTIONS -Wall #-}
import Control.Monad
import Control.Monad.Random
import Data.List
sumDice :: (MonadRandom m) => Int -> m Int
sumDice n = liftM sum $ replicateM n $ getRandomR (1,6)
statDice :: (MonadRandom m) => Int -> m [(Int,Int)]
statDice n = do
@nushio3
nushio3 / MainRand.hs
Created January 5, 2012 08:25
これはどうにかなりませんかねえ ref: http://qiita.com/items/1580
recMapM :: forall a b m. (Data a, Data b, Monad m) => (b -> m b) -> a -> m a
recMapM f x = do
let recurse = gmapM (recMapM f) x
case cast x of
Nothing -> recurse
Just bx -> do
z <- f bx
case cast z of
Nothing -> recurse
Just az -> return az
#!/usr/bin/env runhaskell
{-# LANGUAGE NoImplicitPrelude, RecordWildCards #-}
{-# OPTIONS -Wall #-}
module Main where
import Data.Maybe
import Data.Tensor.TypeLevel
import GTA.Data.JoinList
import GTA.Core hiding (items)
import NumericPrelude
@nushio3
nushio3 / gist:4162351
Created November 28, 2012 16:27
how to treat zippable objects as applicatives.
main = print "hi"
@nushio3
nushio3 / wasshoi.hs
Created November 28, 2012 16:31
test
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
import Control.Applicative
import qualified Data.Vector as V
import Prelude hiding (zipWith)
class Zippable f where
zipWith :: (a->b->c) -> f a -> f b -> f c
@nushio3
nushio3 / sugoi07.md
Last active December 15, 2015 11:29

小問1

(選択肢文字列,値)のリストをとって、ユーザーに選択肢を提示し、番号を入力して選んでもらった値を返す関数

choice :: [(String, a)] -> IO a

を作ってください。

main :: IO ()
main = top 10
top :: Int -> IO ()
top mp = do
putStrLn $ "あなたのMPは:" ++ show mp
putStrLn "どこへ行く?"
choice [ ("宿屋" , inn)
, ("野外" , outdoor mp) ]
gnuplot :: [String] -> IO ()
gnuplot cmds = do
writeFile "tmp" $ unlines cmds
system "gnuplot tmp"
return ()
main = do
gnuplot [ gnuplotSetTerm
@nushio3
nushio3 / arityOf.hs
Last active December 19, 2015 00:09
import Data.Typeable
import Test.Hspec
arityOf :: Typeable a => a -> Int
arityOf x = go $ typeOf x
where
go tr
| isFun $ typeRepTyCon tr = 1 + go (last $ snd $ splitTyConApp tr)
| otherwise = 0