Skip to content

Commit dc38391

Browse files
committed
updated README
1 parent b38c48b commit dc38391

File tree

5 files changed

+108
-7
lines changed

5 files changed

+108
-7
lines changed

FFI/wiki.md renamed to FFI/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
Haskell FFI (Foreign Function Interface) helps calling c functions from haskell and vice versa. You just need to write function signature with appropriate data types (haskell equivalent data types of c).
66

77
## How to run:
8-
8+
```
99
$ ghc ffi_examples.hs ffi_examples_c.hs
1010
$ ./ffi_examples
11+
```
1112

1213
##
13-
You can never return char* from c function. You must create the string in heap (malloc) and then only you can return the string.
14+
You can never return char* from c function. You must create the string in heap (malloc) and then only you can return the string.
1415

15-
## Upcoming functions
16+
Alignment is the LCM of all the sizeof of datatypes in the strucutre/Data constructor.
17+
SizeOf should be the sum of all sizeof of datatypes in the structure includeing the aligment.
1618

19+
## Upcoming functions
1720
1. Take 2 array...return concatenated array
1821
2. Take 2 pointers...return nothing
1922
3. take 2 strings...return one string

FFI/ffi_examples

11.8 KB
Binary file not shown.

FFI/ffi_examples.hs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Foreign.ForeignPtr
99
import Foreign.C.String
1010
import Foreign.Marshal.Array
1111
import System.IO.Unsafe
12+
import Control.Applicative ((<$>), (<*>))
13+
import Foreign.Storable
1214

1315
foreign import ccall "math.h abs" c_abs :: CInt -> CInt
1416
foreign import ccall "math.h pow" c_pow :: CDouble -> CDouble -> CDouble
@@ -20,8 +22,57 @@ foreign import capi "printf" c_printInt :: CString -> CInt -> IO()
2022
foreign import ccall "intToString" c_intToString :: CInt -> CString
2123
foreign import ccall "bubbleSort" c_bubbleSort :: Ptr CInt -> CInt -> IO()
2224
foreign import ccall "toUpper" c_toUpper :: CString -> IO()
23-
foreign import ccall "addTwoArrays" c_addTwoArrays:: Ptr CInt -> CInt -> Ptr CInt -> Int -> IO (Ptr CInt)
24-
foreign import ccall "addThreeArrays" c_addThreeArrays:: Ptr CInt -> CInt -> Ptr CInt -> Int -> Ptr CInt -> Int -> IO (Ptr CInt)
25+
foreign import ccall "addTwoArrays" c_addTwoArrays :: Ptr CInt -> CInt -> Ptr CInt -> Int -> IO (Ptr CInt)
26+
foreign import ccall "addThreeArrays" c_addThreeArrays :: Ptr CInt -> CInt -> Ptr CInt -> Int -> Ptr CInt -> Int -> IO (Ptr CInt)
27+
foreign import ccall "viewStudent" c_viewStudent :: Ptr Student -> IO()
28+
foreign import ccall "viewPerson" c_viewPerson :: Ptr Person -> IO()
29+
30+
data Student = Student {
31+
roll :: Int32,
32+
age :: Int32
33+
}
34+
35+
data Person = Person {
36+
roll2 :: Int32,
37+
ch :: Char,
38+
num :: Int32
39+
}
40+
41+
instance Storable Student where
42+
alignment _ = 4
43+
sizeOf _ = 8
44+
peek ptr = Student
45+
<$> peekByteOff ptr 0
46+
<*> peekByteOff ptr 4
47+
poke ptr (Student r a) = do
48+
pokeByteOff ptr 0 r
49+
pokeByteOff ptr 4 a
50+
51+
instance Storable Person where
52+
alignment _ = 4
53+
sizeOf _ = 12
54+
peek ptr = Person
55+
<$> peekByteOff ptr 0
56+
<*> peekByteOff ptr 4
57+
<*> peekByteOff ptr 8
58+
poke ptr (Person r c n) = do
59+
pokeByteOff ptr 0 r
60+
pokeByteOff ptr 4 c
61+
pokeByteOff ptr 8 n
62+
63+
viewStudent :: Int -> Int -> IO ()
64+
viewStudent x y = do
65+
let st = Student (fromIntegral x) (fromIntegral y)
66+
alloca $ \ptr -> do
67+
poke ptr st
68+
c_viewStudent ptr
69+
70+
viewPerson :: Int -> Char -> Int -> IO ()
71+
viewPerson x y z = do
72+
let pt = Person (fromIntegral x) y (fromIntegral z)
73+
alloca $ \ptr -> do
74+
poke ptr pt
75+
c_viewPerson ptr
2576

2677
swapTwoNums :: Int -> Int -> (Int,Int)
2778
swapTwoNums x y = unsafePerformIO $ do
@@ -104,8 +155,11 @@ main = do
104155
print $ intToString 2
105156
-- pass int*
106157
bubbleSort ([4,6,3,7,1] :: [Int32])
107-
-- toUpper
158+
-- toUpper / pass String
108159
toUpper "tushar"
109160
-- merge array
110161
print $ addTwoArrays [1,2,3] [4,5,6]
111162
print $ addThreeArrays [1,2,3] [4,5,6] [7,8,9]
163+
-- structure
164+
viewStudent 2 3
165+
viewPerson 2 'a' 3

FFI/ffi_examples_c.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22
#include<string.h>
33
#include<stdio.h>
44

5+
typedef struct Student{
6+
int roll;
7+
int age;
8+
}Student;
9+
10+
typedef struct Person{
11+
int roll;
12+
char ch;
13+
int num;
14+
}Person;
15+
16+
void viewPerson(Person* p){
17+
if(!p)
18+
return;
19+
printf("Person: %d %c %d\n",p->roll,p->ch,p->num);
20+
}
21+
22+
void viewStudent(Student* st){
23+
if(!st)
24+
return;
25+
printf("%d %d\n",st->roll,st->age);
26+
}
27+
528
void swap_nums(int* x,int* y){
629
int temp = *x;
730
*x = *y;

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1-
# haskell-examples
1+
# haskell-examples
2+
3+
Haskell is statically typed, purely functional programming language. Despite being so good, One of the biggest problem, newcomers face while learning haskell is the lack tutorials/documenation. The online learning resource for haskell is quite low, compared with other languages such as Java, Python etc. Hence, I am buiilding repo, containing concept-wise examples, which will new programmers to learn the language as well as experinced haskell programmers can use this as a reference mannual.
4+
5+
I will also try to write theory in each section's `README`.
6+
7+
8+
## Contents
9+
- Foreign Functions Interface
10+
11+
## Upcoming Material
12+
- Types
13+
- Typeclasses
14+
- Pattern Matching
15+
- Lists
16+
- Recursion
17+
- Maybe & Either
18+
- Maps
19+
- Functors
20+
- Applicatives
21+
- Monads
22+
- Testing

0 commit comments

Comments
 (0)