From 55ceae09963399fb32b8bc58dda4f4482f3df174 Mon Sep 17 00:00:00 2001 From: Nicola Bernini Date: Thu, 4 Apr 2019 18:19:46 +0200 Subject: [PATCH 1/4] Create readme.md --- haskell/functions/readme.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 haskell/functions/readme.md diff --git a/haskell/functions/readme.md b/haskell/functions/readme.md new file mode 100644 index 0000000..09c9660 --- /dev/null +++ b/haskell/functions/readme.md @@ -0,0 +1,6 @@ + +# Overview + +Some Convenient Haskell Functions developed mostly as exercises + + From 57eae9fdddaf5b62f8c47eb1af8b75f006e4bc2a Mon Sep 17 00:00:00 2001 From: Nicola Bernini Date: Thu, 4 Apr 2019 18:46:35 +0200 Subject: [PATCH 2/4] Create php_explode.hs --- haskell/functions/php_explode.hs | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 haskell/functions/php_explode.hs diff --git a/haskell/functions/php_explode.hs b/haskell/functions/php_explode.hs new file mode 100644 index 0000000..20219f9 --- /dev/null +++ b/haskell/functions/php_explode.hs @@ -0,0 +1,60 @@ + +-- A function which works as PHP Explode +-- https://www.php.net/manual/es/function.explode.php + +-- Lists Slice +-- Arg1: Start Index +-- Arg2: End Index +-- Arg3: Input List +-- Return: Output List +slice :: Int -> Int -> [a] -> [a] +slice from to xs = take (to - from + 1) (drop from xs) + + +-- Is Space +-- Arg1: String +-- Arg2: Position +-- Return: Bool representing if i-th char is a space +is_space :: String -> Int -> Bool +is_space s i = (s!!i == ' ') + +-- Is Char +-- Arg1: String +-- Arg2: Position +-- Return: Bool representing if i-th char is equal to query char +is_char :: String -> Char -> Int -> Bool +is_char s c i = (s!!i == c) + + + +-- Builds a List of N elements each representing the position of a space in the input string +-- Arg1: Input String +-- Return: List of Int representing the positions of space in the string +get_spaces :: String -> [Int] +get_spaces s = [i | i <- [0 .. ((length s)-1)], is_space s i] + +-- Builds a list of N elements each representing the position of the given chan in the input string +find_separators :: String -> Char -> [Int] +find_separators s c = [ i | i <- [ 0 .. ((length s)-1) ], is_char s c i ] + +-- Equivalen to PHP Explode so breaks the input string into a list of substring according to given separator char +explode :: String -> Char -> [String] +explode s c + | d == [] = [s] + | otherwise = initial ++ [ slice ((d!!(i-1))+1) ((d!!i)-1) s | i <- [1 .. ((length d)-1)] ] ++ final + where + d = find_separators s c + initial = [(slice 0 ((d!!0)-1) s)] + final = [(slice ((d!!((length d)-1))+1) ((length s)-1) s)] + + + +-- Tests +main = do + let a = "Hello this is a test of a function" + let b = "Test" + print (explode a ' ') + print (explode b ' ') + + + From c56c6cd033b85e681ec6baff7a7b85e545b666ad Mon Sep 17 00:00:00 2001 From: Nicola Bernini Date: Thu, 4 Apr 2019 18:48:47 +0200 Subject: [PATCH 3/4] Update readme.md --- haskell/functions/readme.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/haskell/functions/readme.md b/haskell/functions/readme.md index 09c9660..96e297e 100644 --- a/haskell/functions/readme.md +++ b/haskell/functions/readme.md @@ -3,4 +3,19 @@ Some Convenient Haskell Functions developed mostly as exercises +# String Manipulation + +## PHP Explode + +Build a function that as [PHP Explode](https://www.php.net/manual/es/function.explode.php) takes a string and breaks it up into a list of substring according to given separator char +- [Sol](php_explode.hs) + + + + + + + + + From de7f851a65a4ec51e42db10f96856d159632f6e1 Mon Sep 17 00:00:00 2001 From: Nicola Bernini Date: Thu, 4 Apr 2019 18:49:50 +0200 Subject: [PATCH 4/4] Update readme.md --- haskell/functions/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/haskell/functions/readme.md b/haskell/functions/readme.md index 96e297e..020f806 100644 --- a/haskell/functions/readme.md +++ b/haskell/functions/readme.md @@ -11,7 +11,7 @@ Build a function that as [PHP Explode](https://www.php.net/manual/es/function.ex - [Sol](php_explode.hs) - +Work in progress