Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Scheme Programming Language

hmmm ... a solution but not a high order procedure.


Hi all,

Before I look at any replies I thought I might give it another go.

The original objective was to create a higher order procedure using a
procedure as a param to construct a list of words contained in a string.

While I have a solution, it is not a higher order procedure of the type I
was hoping for.  It seemed to me that the task didn't lend itself to this
approach - your comments may prove me completely wrong (most likely).

I see no value of making a fake higher order procedure by placing
definitions inside the master procedure simply to show it can be done.  I
see more value having the contributing procedures kept outside the main one
because if necessary, these can be modified to get different results.  Be
interested in alternate thoughts about this.

I'm certain there are many ways to solve the problem - but I wanted to avoid
using more than the basic Scheme procedures.

I'd be interested in feedback on style / problem solving approach.  I have
yet to optimise the code but I wanted to make certain that what was
happening was easy to read (and worked!).

Minor adjustments can result in spaces being included either as individual
string elements containing one space or n spaces.

Now to see what others wrote.

Mike
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Problem: convert a string (sentence)
;                into a list of strings (word and punctuation)
;                excluding space characters.
;
; Expected Result: ("This" "is" "a" "sentence" ".")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; CONSTANTS
;
(define a-list (string->list "This is    a  sentence   ."))
;;;;;;;;;;;;;;;;;;;;;;;;
(define [space? x]
  (eq? x #\space))
(define [make-word a-list] ; returns a list of chars (word)
  (cond
    [(empty? a-list) empty]
    [(space? (car a-list)) empty]
    [else
     (cons (car a-list) (make-word (cdr a-list)))]))
(define [strip-word a-list]
  (cond
    [(empty? a-list) empty]
    [(space? (car a-list)) (cdr a-list)]
    [else
     (strip-word (cdr a-list))]))
(define [make-string a-list]
  (list->string a-list))
(define [make-sentence a-list]

  (cond
    [(empty? a-list) empty] ; Nothing left.  Stop.
    [(space? (car a-list)) (make-sentence (cdr a-list))]
;                            ; list isn't empty.
;                            ; strip spaces
    [else
       (cons (make-string (make-word a-list))
           (make-sentence (strip-word a-list)))]))
; TEST
(make-sentence a-list)
; returns
;==> ("This" "is" "a" "sentence" ".")

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc