Skip to content

Commit 111fbb3

Browse files
committed
Few FFI examples, Readme, BinarySearch
1 parent dc38391 commit 111fbb3

File tree

8 files changed

+54
-3
lines changed

8 files changed

+54
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.o
22
*.hi
3+
*.exe

FFI/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
[https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/ffi.html]
2-
[https://en.wikibooks.org/wiki/Haskell/FFI]
3-
[https://wiki.haskell.org/Foreign_Function_Interface]
1+
[resource1](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/ffi.html)
2+
3+
[resource2](https://en.wikibooks.org/wiki/Haskell/FFI)
4+
5+
[resource3](https://wiki.haskell.org/Foreign_Function_Interface)
46

57
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).
68

FFI/ffi_examples.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ foreign import ccall "addTwoArrays" c_addTwoArrays :: Ptr CInt -> CInt -> Ptr CI
2626
foreign import ccall "addThreeArrays" c_addThreeArrays :: Ptr CInt -> CInt -> Ptr CInt -> Int -> Ptr CInt -> Int -> IO (Ptr CInt)
2727
foreign import ccall "viewStudent" c_viewStudent :: Ptr Student -> IO()
2828
foreign import ccall "viewPerson" c_viewPerson :: Ptr Person -> IO()
29+
foreign import ccall "getStudent" c_getStudent :: CInt -> CInt -> IO(Ptr Student)
2930

3031
data Student = Student {
3132
roll :: Int32,
@@ -47,6 +48,9 @@ instance Storable Student where
4748
poke ptr (Student r a) = do
4849
pokeByteOff ptr 0 r
4950
pokeByteOff ptr 4 a
51+
instance Show Student where
52+
show st = "Roll number: " ++ show (roll st) ++ " Age: " ++ show (age st)
53+
5054

5155
instance Storable Person where
5256
alignment _ = 4
@@ -67,6 +71,13 @@ viewStudent x y = do
6771
poke ptr st
6872
c_viewStudent ptr
6973

74+
getStudent :: Int -> Int -> IO ()
75+
getStudent r a = do
76+
let stPtr = unsafePerformIO $ c_getStudent (fromIntegral r) (fromIntegral a)
77+
foreignPtr <- newForeignPtr p_free stPtr
78+
st <- withForeignPtr foreignPtr $ \ptr -> peek ptr
79+
print st
80+
7081
viewPerson :: Int -> Char -> Int -> IO ()
7182
viewPerson x y z = do
7283
let pt = Person (fromIntegral x) y (fromIntegral z)
@@ -163,3 +174,5 @@ main = do
163174
-- structure
164175
viewStudent 2 3
165176
viewPerson 2 'a' 3
177+
getStudent 2 3
178+

FFI/ffi_examples_c.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ void viewStudent(Student* st){
2525
printf("%d %d\n",st->roll,st->age);
2626
}
2727

28+
Student* getStudent(int roll,int age){
29+
Student* st = malloc(sizeof(struct Student*));
30+
st->roll = roll;
31+
st->age = age;
32+
return st;
33+
}
34+
2835
void swap_nums(int* x,int* y){
2936
int temp = *x;
3037
*x = *y;

Recursion/BinarySearch.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module BinarySearch where
2+
import Data.Int
3+
4+
binarySearch_ :: [Int32] -> Int32 -> Int -> Int -> Maybe Int
5+
binarySearch_ list1 elem low high
6+
| low >= high = Nothing
7+
| (list1 !! mid) < elem = binarySearch_ list1 elem (mid+1) high
8+
| (list1 !! mid) > elem = binarySearch_ list1 elem low (mid-1)
9+
| otherwise = Just mid
10+
where
11+
mid = low + ((high-low) `div` 2)
12+
13+
binarySearch :: [Int32] -> Int32 -> Maybe Int
14+
binarySearch list1 elem = binarySearch_ list1 elem 0 (length list1)

Recursion/Fibonacci.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module Fibonacci where

Recursion/Main.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Main where
2+
import BinarySearch
3+
4+
main :: IO()
5+
main = do
6+
let list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
7+
let elem = 7
8+
print $ binarySearch list1 elem

Recursion/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[Resource1](http://www.jacobsheehy.com/2009/03/binary-search-in-haskell/)
2+
3+
Recursion is a crucial part of haskell. I sure you already what recursion is...
4+
5+
In the examples, I have used lists, but for performance improvements you can use [Arrays](https://hackage.haskell.org/package/array-0.5.6.0/docs/Data-Array.html).

0 commit comments

Comments
 (0)