This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 関数適用演算子$ | |
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]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 課題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)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 複数のネットワークインターフェイスを持つホストもあるので、すべての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); //重要 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 末尾再帰についての説明 | |
/*********************************************************/ | |
/* 処理概要: nの階乗を返す (Int -> Int) */ | |
/* 目的: 普通の再帰で書いたものと、末尾再帰で書いたものの比較 */ | |
/*********************************************************/ | |
// =====普通の再帰===== | |
int fact(int n) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 目的: 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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) |