Advertisement
triclops200

Scheme

Jul 8th, 2013
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. ltype = type([])
  2.  
  3. class NonCallableError(Exception):
  4.     def __init__(self,st):
  5.         self.st = st
  6.     def __str__(self):
  7.         return self.st + " is not a callable object."
  8.                
  9. def filter(conditional,xs):
  10.     return [x for x in xs if conditional(x)]
  11. def map(function,xs):
  12.     return [function(x) for x in xs]
  13. def reduce(funtction,xs,start):
  14.     for x in xs:
  15.         start = function(start,x)
  16.     return start
  17. def is_number(s):
  18.     try:
  19.         float(s)
  20.         return True
  21.     except ValueError:
  22.         return False
  23. def s_parse(s,index=0):
  24.     xs = []
  25.     inquotes = False
  26.     escaped = False
  27.     word = ""
  28.     while index < len(s):
  29.         if s[index] == "(" and not inquotes:
  30.             inner,index = s_parse(s,index+1)
  31.             xs.append(inner)
  32.         elif s[index] == '"' and not escaped and not inquotes:
  33.             inquotes = True
  34.             word = '"'
  35.         elif s[index] == '"' and not escaped and inquotes:
  36.             inquotes = False
  37.             word += '"'
  38.         elif s[index] == " " and not inquotes:
  39.             xs.append(word)
  40.             word = ""
  41.         elif s[index] == ")" and not inquotes:
  42.             xs.append(word)
  43.             xs = filter(lambda word: word != "",xs)
  44.             return xs,index
  45.         else:
  46.             word += s[index]
  47.         index += 1
  48.     return xs[0]
  49.  
  50.  
  51.  
  52. print(s_parse('(1 2 "stenrasotoin oarnt ()()(" (3 4 5 (6)) 7)'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement