Skip to content

Instantly share code, notes, and snippets.

View chomado's full-sized avatar
🎀
Working

ちょまど | Madoka Chiyoda chomado

🎀
Working
View GitHub Profile
@chomado
chomado / bot.cs
Created April 27, 2016 11:48
Microsoft の Bot Framework を使って bot を作った。その bot の本体部分の記述
public async Task<Message> Post([FromBody]Message message)
{
if (message.Type == "Message")
{
// calculate something for us to return
int length = (message.Text ?? string.Empty).Length;
// return our reply to the user
return message.CreateReplyMessage($"You sent {length} characters");
}
-- fact 5
-- → 5 * 4 * 3 * 2 * 1
fact :: Int -> Int
fact 0 = 1
fact x = x * fact (x - 1)
main :: IO()
main = do
print $ fact 5 -- 120
@chomado
chomado / fib.hs
Last active August 29, 2015 14:08
import Debug.Trace
fib :: Num a => [a]
fib = 0:1:(trace ("fib [" ++ show fib ++ "] + tail fib [" ++ show (tail fib) ++ "]")
(zipWith (+) fib (tail fib)))
main = do
traceIO $ show $ take 10 fib
fib :: Num a => [a]
fib = 0:1:zipWith (+) fib (tail fib)
import Debug.Trace
-- trace (表示させたい文字列) (処理return)
f x = trace ("x = " ++ show x) x + 1
main = do
traceIO $ show $ f 5
-- x = 5
-- 6
-- フィボナッチ数列生成のためのテスト関数
fibo :: (Int -> Int) -> Int -> [Int]
fibo func n = [func x| x <- [0..n]]
-- パターンマッチで実装
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
insert :: Ord a => a -> [a] -> [a]
insert x [] = [x]
insert x (y : ys)
| x <= y = x:y:ys
| otherwise = y: insert x ys
isort :: Ord a => [a] -> [a]
isort [] = []
isort (x:xs) = insert x $ isort xs
qsort :: Ord a => [a] -> [a]
qsort [] = []
qsort (pivot:xs) = qsort smaller ++ [pivot] ++ qsort larger
where
smaller = [x | x <- xs, x < pivot]
larger = [x | x <- xs, x >= pivot]
main = do
print $ qsort [4, 6, 9, 8, 3, 5, 1, 7, 2]
-- [1,2,3,4,5,6,7,8,9]
import Data.Char
-- 文字c を シフト数n だけずらす関数
shift :: Int -> Char -> Char
shift n c
| isLower c = chr $ ord 'a' + ((ord c - ord 'a') + n) `mod` 26
| isUpper c = chr $ ord 'A' + ((ord c - ord 'A') + n) `mod` 26
| otherwise = c
--シーザー暗号化する関数
@chomado
chomado / list.hs
Last active August 29, 2015 14:08
length' :: [a] -> Int
length' [] = 0
length' (x:xs) = 1 + length' xs
----------
take' :: Int -> [a] -> [a]
take' _ [] = []
take' 0 _ = []
take' n (x:xs) = x : take' (n-1) xs