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 / kansuutekiyouenzanshi.hs
Last active December 18, 2015 17:09
Lispみたいな式を簡単に書く練習メモ(すごいHaskell本より)
-- 関数適用演算子$
sum (filter (> 10) (map (* 2) [ 2 .. 10]))
sum $ filter (> 10) $ map (* 2) [ 2 .. 10]
-- 関数合成.
map (\x -> negate (abs x)) [5, -3, -6, 7, -3, 2, -19, 24]
map (negate . abs) [5, -3, -6, 7, -3, 2, -19, 24]
map (\xs -> negate (sum (tail xs))) [[1..5], [3..6], [1..7]]
map (negate . sum . tail) [[1..5], [3..6], [1..7]]
mynub :: Eq a => [a] -> [a]
mynub [] = []
mynub (x : xs) = x : mynub (filter (\y -> x /= y) xs)
-- nub :: Eq a => [a] -> [a]
-- Input: nub [0,1,2,3,2,1,0] 重複するものを取り除く
-- Output: [0,1,2,3]
-- filter (>3) [3, 5, 9, 10, 3, 1, 0, 3, 5]
-- [5,9,10,5]
@chomado
chomado / average.c
Last active December 19, 2015 19:18
課題1。整数データを入力して、平均値、最小値、最大値を表示するCのプログラムを書いてください。 整数データの範囲は0以上1000以下とします。 ファイル名はaverage.cとしてください
/* 課題1 */
/* 整数データを入力して、平均値、最小値、最大値を表示するCのプログラムを書いてください。 整数データの範囲は0以上1000以下とします。 ファイル名はaverage.cとしてください。*/
#include <stdio.h>
#include <math.h>
int main() {
int n, x;
int min = 1000, max = 0, count = 0;
double ave, sum = 0;
while(scanf("%d", &x)!=EOF && (x >= 0 && x <= 1000)) {
@chomado
chomado / Connect.java
Created October 23, 2013 14:54
簡単なWebクライアント
import java.net.*;
public class Connect {
public static void main(String[] args) {
String server_name = "localhost";
if (args.length > 0) server_name = args[0];
try {
Socket sock = new Socket(server_name, 80);
System.out.println("Connected to " + server_name);
// do something
sock.close();
@chomado
chomado / GetIPAddress.java
Last active December 26, 2015 10:39
アドレスを調べる
import java.net.*;
public class GetIPAddress {
public static void main(String[] args) {
String server_name = "localhost";
if (args.length > 0) {
server_name = args[0];
}
try {
InetAddress ia = InetAddress.getByName(server_name);
System.out.println("host name: " + ia.getHostName());
@chomado
chomado / GetIPAddress2.java
Created October 24, 2013 15:14
複数のネットワークインターフェイスを持つホストのすべてのIPアドレスを取得する。
// 複数のネットワークインターフェイスを持つホストもあるので、すべてのIPアドレスを取得する。
import java.net.*;
public class GetIPAddress2 {
public static void main(String[] args) {
String server_name = "localhost";
if (args.length > 0) {
server_name = args[0];
}
try {
InetAddress[] ia = InetAddress.getAllByName(server_name); //重要
@chomado
chomado / DaytimeText.java
Last active December 26, 2015 10:59
localhostの13番ポートで動作しているDaytimeサーバに接続するクライアント
import java.net.*;
import java.io.*;
public class DaytimeText {
boolean isOK = false;
String host;
Socket sock;
public DaytimeText(String host, int port) {
try {
this.host = host;
sock = new Socket(host, port);
// 末尾再帰についての説明
/*********************************************************/
/* 処理概要: nの階乗を返す (Int -> Int) */
/* 目的: 普通の再帰で書いたものと、末尾再帰で書いたものの比較 */
/*********************************************************/
// =====普通の再帰=====
int fact(int n)
{
(* 目的: init から始めてlstの要素を右から順にfを施し込む *)
(* fold_right: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b *)
let rec fold_right f lst init = match lst with
[] -> init
| first :: rest -> f first (fold_right f rest init)
@chomado
chomado / tailCall.hs
Created February 24, 2014 16:41
「なんで末尾呼び出しだと使うメモリの量が減るのか可視化してみた」by @naohaq さん
-- http://na.yuki.st/src/tailcall.txt より。
-- 末尾呼出でない定義
fact 0 = 1
fact n = n * fact (n - 1)
-- 末尾呼出な定義
fact' a 0 = a
fact' a n = fact' (a * n) (n - 1)