• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Breaking/Splitting the list with certain number of elem...

Stats

  • Locked Locked
  • Replies 7
  • Subscribers 144
  • Views 3993
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Breaking/Splitting the list with certain number of elements

Saikrishna14
Saikrishna14 over 10 years ago
Hi
I am looking for something opposite to the function of append.I want to break a very big list into smaller lists of elements.For example say i have 103 elements in a list and i want to break the list into sublists such that each list consists of 20 elements.I should get an output in return with 5 sublists of 20 elements each and a 6th sublist of 3 elements.Is there any direct function available for use?


   
  • Cancel
  • tweeks
    tweeks over 10 years ago

    Saikrishna14 said:

    Is there any direct function available for use?

    No.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • tweeks
    tweeks over 10 years ago
    (inScheme
    
     (defun CCFfoldr (f z xs "ugl")
       (if (null xs)
           z
           (f (car xs) (foldr f z (cdr xs)))))
    
     (defun CCFbreakListr (xs subLen "lx")
       (and (lessp subLen 1)
            (syntax_error 'CCFbreakListr "2nd argument must be greater than zero" subLen))
       (defun consit (x y)
         (cond ((null y)
                (ncons (ncons x)))
               ((eqv (length (car y)) subLen)
                (cons (ncons x) y))
               (t
                (cons (cons x (car y)) (cdr y)))))
       (CCFfoldr consit nil xs))
    
     ;; OP's example
     (let (bigList)
       (for i 1 103 (push i bigList))
       (setq bigList (reverse bigList))
       (CCFbreakListr bigList 20))
    
    ) ; end inScheme
    
    ;; return value:
    /*
    
    ((1 2 3) 
        (4 5 6 7 8
    	9 10 11 12 13
    	14 15 16 17 18
    	19 20 21 22 23
        ) 
        (24 25 26 27 28
    	29 30 31 32 33
    	34 35 36 37 38
    	39 40 41 42 43
        ) 
        (44 45 46 47 48
    	49 50 51 52 53
    	54 55 56 57 58
    	59 60 61 62 63
        ) 
        (64 65 66 67 68
    	69 70 71 72 73
    	74 75 76 77 78
    	79 80 81 82 83
        )
        (84 85 86 87 88
    	89 90 91 92 93
    	94 95 96 97 98
    	99 100 101 102 103
        )
    )
    
    */
    
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • tweeks
    tweeks over 10 years ago

    Here's one that goes the other way:

    (inScheme
    
    (defun CCFfoldl (f z xs "ugl")
      (let ((result z))
        (foreach mapc x xs
          (setq result (f result x)))
        result))
    
     (defun CCFbreakListl (xs subLen "lx")
       (and (lessp subLen 1)
            (syntax_error 'CCFbreakListl "2nd argument must be greater than zero" subLen))
       (defun consit (x y)
         (cond ((null x)
                (ncons (ncons y)))
               ((eqv (length (car x)) subLen)
                (cons (ncons y) x))
               (t
                (cons (cons y (car x)) (cdr x)))))
       (reverse (mapcar reverse (CCFfoldl consit nil xs))))
    
     ;; OP's example
     (let (bigList)
       (for i 1 103 (push i bigList))
       (setq bigList (reverse bigList))
       (CCFbreakListl bigList 20))
    
    ) ; end inScheme
    
    ;; return value:
    
    /*
    ((1 2 3 4 5
    	6 7 8 9 10
    	11 12 13 14 15
    	16 17 18 19 20
        ) 
        (21 22 23 24 25
    	26 27 28 29 30
    	31 32 33 34 35
    	36 37 38 39 40
        ) 
        (41 42 43 44 45
    	46 47 48 49 50
    	51 52 53 54 55
    	56 57 58 59 60
        ) 
        (61 62 63 64 65
    	66 67 68 69 70
    	71 72 73 74 75
    	76 77 78 79 80
        ) 
        (81 82 83 84 85
    	86 87 88 89 90
    	91 92 93 94 95
    	96 97 98 99 100
        )
        (101 102 103)
    )
    */
    

    Note CCFbreakListr consumes runtime stack space proportional to the length of the list, so it will fail on very long lists.  CCFbreakListl does not have this deficiency, but it is not as fast as it could be due to the gratuitous use of reverse. A more efficient implementation using tconc is left as an exercise for the reader.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • theopaone
    theopaone over 10 years ago

    Bravo, Tom

    You introduced the concept of passing a function f to the CCFfold1 function and evaluating it: (f  result  x).

    This is high wizardry, you demonstrate a mastery of your craft.

    Ted

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • tweeks
    tweeks over 10 years ago
    theopaone said:

    the CCFfold1 function

    That last letter is actually supposed to be an L, not a 1.  (I just took Haskell's left and right fold functions and stuck a "CCF" on the front.) 

    Back in the 80's when SKILL was born, folding seems to have been a relatively uncommon pastime, mostly practiced by academics and fringe functional programmers, which probably explains why there is no built-in in SKILL for it.  (Common Lisp has a built-in fold called REDUCE, named after the corresponding operator in APL, which further goes to show how uncommon this technique was in those days: you had to look to APL for an example!)

    Recently, however, folding seems to have become more popular.  The Wikipedia article is quite good.

    theopaone said:

    You introduced the concept of passing a function f to the CCFfold1 function and evaluating it: (f result x).

    This is high wizardry, you demonstrate a mastery of your craft.

    It's funny you should describe it that way, because I learned it from the Wizard Book! :)

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • FormerMember
    FormerMember over 8 years ago

    thanks for wonderful code.

    I have a question about "ugl" and "lx" in defun.

    what do they stand for?

    I guess "u" means function. Is it right?

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 8 years ago

    These are the optional type-checking argument. If the last argument to a function definition (defun, procedure etc) is a string, then each character encodes the allowed type of the corresponding argument position. If there are too few type characters, the last is repeated for any subsequent arguments. Here's some of the common type letters (they are in the SKILL User Guide if memory serves me correctly):


    R : a ROD object
    S : either a string or a symbol
    U : a function object
    a : an array
    b : a ddUserType
    d : a database object
    e : an environment
    f : a floating point number
    g : generic - anything matches
    l : a list
    n : a number
    o : any user-defined (other) type
    p : an I/O port
    r : a defstruct
    s : a symbol
    t : a string
    u : a function object or name
    x : an integer
    y : a binary function

    So "ugl" means a function followed by anything followed by a list. "lx" means a list then an integer.

    There's also a typo in the first code - the foldr should be CCFfoldr. Obviously when Tom felt bad about not prefixing his functions and fixed them after the event ;-)

    Regards,

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information