0

I'm having trouble declaring multiple functions after my initial function for where.

My function I am attempting to fix:

--initials function using where and pattern matching
initials :: String -> String -> String
initials firstname lastname = [f] ++ ". " ++ [l] ++ "."
    where (f:_) = firstname
          (l:_) = lastname

I currently run the file in ghci by the command :l functionSyntax as the file is named functionSyntax.hs.

What am i doing wrong here? The site I am learning haskell from, LearnYouaHaskell, has their code written as:

initials :: String -> String -> String  
initials firstname lastname = [f] ++ ". " ++ [l] ++ "."  
    where (f:_) = firstname  
          (l:_) = lastname   

which I feel is the exact same code, but I'm receiving the error:

ghci> :l functionSyntax
[1 of 1] Compiling Main             ( functionSyntax.hs, interpreted )

functionSyntax.hs:86:31: parse error on input `='
Failed, modules loaded: none.

I've tried looking up this error as it seems pretty common, but I can't seem to fix this seemingly simple problem. Any help is appreciated!

Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177
  • 2
    Most likely an indentation issue with the `where` clause. The code runs fine on my machine. Which line is 86? As an aside, you should probably use `head`, which is a function which takes a list and returns the first element, instead of pattern matching the two arguements. – user2407038 Jun 05 '13 at 01:37
  • 10
    Are you mixing tabs and spaces which is the most common problem for such cases. – Satvik Jun 05 '13 at 02:07
  • 1
    I copy and pasted your code in a file called functionSyntax.hs, then I loaded the file into ghci just like you did, and your code works fine for me. – 7stud Jun 05 '13 at 05:00
  • Why not simply `initials (f:_) (l:_) = [f] ++ ". " ++ [l] ++ "."` ? – Landei Jun 05 '13 at 07:31

0 Answers0