### changeset 26:0f4ccdbaf57f

author Masataka Kohagura Sun, 11 May 2014 03:49:24 +0900 8c7e1b34582f 2a50f327e405 Haskell/baby.hs c/mmap/Makefile c/mmap/main.cc 3 files changed, 186 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
```--- a/Haskell/baby.hs	Wed Mar 26 18:13:52 2014 +0900
+++ b/Haskell/baby.hs	Sun May 11 03:49:24 2014 +0900
@@ -48,3 +48,60 @@
++ case ls of [] -> "empty."
[x] -> "a singleton list."
xs -> "a longer list."
+
+maximum' :: (Ord a) => [a] -> a
+maximum' [] = error "maximum of empty list!"
+maximum' [x] = x
+maximum' (x:xs) = max x (maximum' xs)
+
+replicate' :: Int -> a -> [a]
+replicate' n x
+    | n <= 0    = []
+    | otherwise = x : replicate' (n-1) x
+
+take' :: Int -> [a] -> [a]
+take' n _
+    | n <= 0    = []
+take' _ []      = []
+take' n (x:xs) = x : take' (n-1) xs
+
+reverse' :: [a] -> [a]
+reverse' [] = []
+reverse' (x:xs) = reverse' xs ++ [x]
+
+repeat' :: a -> [a]
+repeat' x = x : repeat' x
+
+quicksort :: (Ord a) => [a] -> [a]
+quicksort [] = []
+quicksort (x:xs) =
+    let smallerOrEqual = [a| a<-xs, a<= x]
+        larger = [a | a<-xs, a>x]
+    in  quicksort smallerOrEqual ++ [x] ++ quicksort larger
+
+-- Chapter 5
+
+multThree :: Int -> Int -> Int -> Int
+multThree x y z = x * y * z
+
+compareWithHundred :: Int -> Ordering
+compareWithHundred x = compare 100 x
+
+compareWithHundred' :: Int -> Ordering
+compareWithHundred' = compare 100
+
+zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
+-- 型推論をさせると zipWith' :: (t -> t1 -> a) -> [t] -> [t1] -> [a] となる
+zipWith' _ [] _ = []
+zipWith' _ _ [] = []
+zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
+
+flip' :: (a -> b -> c) -> (b -> a -> c)
+flip' f = g
+    where g x y = f y x
+
+chain :: Integer -> [Integer]
+chain 1 = [1]
+chain n
+    | even n = n : chain (n `div` 2)
+    | odd  n = n : chain (n * 3 + 1)```
```--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/c/mmap/Makefile	Sun May 11 03:49:24 2014 +0900
@@ -0,0 +1,10 @@
+TARGET= mmap
+OPTION= -Wall -O0 -g
+
+\$(TARGET):main.cc
+	clang \$(OPTION) -o \$(TARGET) main.cc
+
+clean:
+	rm -f \$(TARGET)
+	rm -r \$(TARGET).dSYM
+	rm -f *~ \#*```
```--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/c/mmap/main.cc	Sun May 11 03:49:24 2014 +0900
@@ -0,0 +1,119 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdbool.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+
+static double st_time;
+static double ed_time;
+
+/*
+ */
+const char *usr_help_str = "Usage : ./read_lseek [-file filename] [-b -d]";
+bool bulk_flag = true;
+bool divide_flag = false;
+
+void result_printf(int,char *);
+
+static double
+get_time(){
+    struct timeval tv;
+    gettimeofday(&tv,NULL);
+    return tv.tv_sec + (double)tv.tv_usec*1e-6;
+}
+
+int main(int argc, char *argv[]){
+
+    struct stat sb;
+
+    char *filename = 0;
+    for (int i = 1; argv[i]; ++i) {
+        if (strcmp(argv[i], "-file") == 0){
+            filename = argv[i+1];
+        }else if(strcmp(argv[i], "-b") == 0){
+            bulk_flag = true;
+            divide_flag = false;
+        }else if(strcmp(argv[i], "-d") == 0){
+            bulk_flag = false;
+            divide_flag = true;
+        }
+    }
+
+    if (filename == 0){
+        puts(usr_help_str);
+        exit(1);
+    }
+
+    st_time = get_time();
+
+    int fd = -1;
+    if ((fd=open(filename,O_RDONLY,0666))==0){
+        fprintf(stderr,"can't open %s\n",filename);
+    }
+
+    if (fstat(fd,&sb)){
+        fprintf(stderr,"can't open %s\n",filename);
+    }
+
+    r.text_size = sb.st_size;
+    char *text = (char *)malloc(sizeof(char)*r.text_size);
+
+
+
+    if(bulk_flag == true){
+    }else if(divide_flag == true){
+    }
+
+    free(text);
+    close(fd);
+
+    ed_time = get_time();
+    printf("Time: %0.6f\n",ed_time-st_time);
+
+    return 0;
+}
+
+/* lseekで読む場所を指定してあげないといけないと思ったが、
+ */
+    for(int i = 0; i < _r.task_num; i++){
+        result_printf(i,_text);
+    }
+    printf("text size    :%lu\n",_r.text_size);
+}
+