fork download
  1.  
  2. -- test Comment
  3. -- String algebra:
  4. name = "Alice"
  5. -- "if" has a special syntax but otherwise a typed version of Lisp's "if":
  6. name2 = if name /= "" then name else "no name"
  7.  
  8. pname3 = print "Bob"
  9.  
  10. myprogram = print (1 + m) -- compiler error: m undefined
  11. m = 1 -- unless this line is also present
  12.  
  13. -- sequencing several imperative programs:
  14. prg1 = do
  15. print "hello "
  16. print name -- level of indentation is important
  17.  
  18. -- the same, but using algebra of imperative programs:
  19. prg1' = sequence_ [print "hello ", print name]
  20.  
  21. -- one imperative program passing value to another:
  22. prg2 = do
  23. line <- getLine
  24. putStrLn ("you typed: " ++ line)
  25.  
  26. main =
  27. do
  28. putStrLn name -- like print, but only for strings
  29. -- putStrLn pname3 -- Couldn't match type ...; Expected type: String; Actual type: IO ()
  30. pname3 -- in Lisp: eval pname3
  31. myprogram; prg1; prg2 -- sequencing, like 3 lines
  32.  
  33.  
  34. -- generic string conversion, using a Java-friendly name:
  35. toString value = show value
  36.  
  37. -- string concatenation:
  38. greet1 = "hello " ++ name ++ (toString 123)
  39. -- the same, but with the operator as a function (like Lisp):
  40. greet2 = (++) "hello " name
  41. -- concatenating more than 2 strings:
  42. greet3 = concat ["hello ", name, toString 123]
  43.  
Success #stdin #stdout 0s 5320KB
stdin
Alice
stdout
Alice
"Bob"
2
"hello "
"Alice"
you typed: Alice