Skip to content

Instantly share code, notes, and snippets.

@jpierson
Last active August 2, 2017 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpierson/88a1be40c2a3eab83d2d271f78293ed6 to your computer and use it in GitHub Desktop.
Save jpierson/88a1be40c2a3eab83d2d271f78293ed6 to your computer and use it in GitHub Desktop.
Experimentation with the Elm language

Elm experiments

Lists

Join a list of strings into a single string with specified separator

import Html exposing (text)

v = [1,2,3]

joinHelper : String -> List String -> String -> String
joinHelper separator list s = 
  case list of
    [] -> s
    h::t -> joinHelper separator t (s ++ separator ++ h)

join : String -> List String -> String
join separator list = 
  case list of 
    [] -> ""
    h::t -> joinHelper separator t h

main =
  text (toString (join "::" (List.map (\i -> toString i) v)))

Outputs the text 1::2::3

Join a list of strings into a single string with specified separator, prefix and suffix

import Html exposing (text)

v = [1,2,3]

joinHelper : String -> String -> List String -> String -> String
joinHelper separator suffix list s = 
  case list of
    [] -> s ++ suffix
    h::t -> joinHelper separator suffix t (s ++ separator ++ h)

join : String -> String -> String -> List String -> String
join prefix separator suffix list = 
  case list of 
    [] -> ""
    h::t -> joinHelper separator suffix t (prefix ++ h)

main =
  text (toString (join "<<<" "::" ">>>" (List.map (\i -> toString i) v)))

Outputs the text <<<1::2::3>>>

Join items of a list using generic function using specified prefix, separator, and suffix

import Html exposing (text)

v = ['a', 'b', 'c']

joinHelper : (a -> a -> a) -> a -> a -> List a -> a -> a
joinHelper append separator suffix list s = 
  case list of
    [] -> append s suffixstrings
    h::t -> joinHelper append separator suffix t (append s (append separator h))

join : (a -> a -> a) -> a -> a -> a -> List a -> a
join append prefix separator suffix list = 
  case list of 
    [] -> append prefix suffix
    h::t -> joinHelper append separator suffix t (append prefix h)
    
myJoinStrings = join (\ a b -> a ++ b) "<<<" "::" ">>>"

main =
  text (toString (myJoinStrings (List.map (\c -> String.fromChar c) v)))

Outputs the text <<<a::b::c>>>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment