Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Haskell functions 20190405 1832 1 #8

Merged
merged 4 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions haskell/functions/php_explode.hs
Original file line number Diff line number Diff line change
@@ -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 ' ')



21 changes: 21 additions & 0 deletions haskell/functions/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

# Overview

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)


Work in progress