first.go 491 B

1234567891011121314151617181920212223
  1. package zygo
  2. // START OMIT
  3. // Go: see https://github.com/glycerine/zygomys/blob/master/repl/functions.go
  4. //
  5. func FirstFunction(env *Glisp, name string, args []Sexp) (Sexp, error) {
  6. if len(args) != 1 {
  7. return SexpNull, WrongNargs
  8. }
  9. switch expr := args[0].(type) {
  10. case *SexpPair:
  11. return expr.Head, nil
  12. case *SexpArray:
  13. if len(expr.Val) > 0 {
  14. return expr.Val[0], nil
  15. }
  16. return SexpNull, fmt.Errorf("first called on empty array")
  17. }
  18. return SexpNull, WrongType
  19. }
  20. // END OMIT