Jump to content

Argument Question (simple problem)


Recommended Posts

Dear friends,

 

I'm still learning. This is a simple thing.

How can I put inside a function the name of a variable, like the example below.

 

I want to do this:

 

(gen-binary-row 12 '(0 2 5 7 8 11))

>> (1 0 1 0 0 1 0 1 1 0 0 1)

 

But in this way:

 

(setf binrow '(0 2 5 7 8 11))
(gen-binary-row 12 '(binrow))

 

What am I missing ?

 

Thanks for help.

Best,

Julio

Link to post
Share on other sites

You can define local variables within and outside functions with let.

 

In principle, you can also use setf within a function, but then you are overwriting the value of a global variable. In most cases you want to avoid that. Functions that do not change anything outside them (so called side effects) are much safer. 

 

Both let and setf are explained in detail in the very informative online textbook Practical Common Lisp, chapter 6: http://www.gigamonkeys.com/book/variables.html. There you will also learn that Lisp actually distinguishes between lexical and dynamic variables; I was talking here about local and global variables instead for simplicity. In case this chapter covers something you don't follow, just go back a chapter or two and you will learn a lot :)

 

Simple example:


(defun my-function (x)
  (let ((y (+ x 1)))
    y)) 

(my-function 42)
; => 43

 

Best,

Torsten 

Link to post
Share on other sites
But in this way:

(setf binrow '(0 2 5 7 8 11))
(gen-binary-row 12 '(binrow))

What am I missing ?

 

just do it like that:


(setf binrow '(0 2 5 7 8 11))
(gen-binary-row 12 binrow)

;; your "binrow" is now a variable with a LIST as value 
;; when you are writing '(binrow) it will be a LIST with the VALUE binrow (and not the values of "binrow")

 

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Similar Topics

    • By PatrickMimran
      Hello 
       
      I would like to know what the more efficient way , when creating a lot of  custom function to be included in the Extension folder:
       
      to group all the function in the same file
       
      or to distribute them over several files .  Or one function by file or it doesn't matter 
       
      Thank you   Patrick
    • By NagyMusic
      I understand that the first argument value in the ambitus-chord function represents the outer-interval size measured by an integer or a list of integers. What does the second value in the size argument list affect when processing two [or more] chords, as illustrated in the example below? Is the second value affecting the second chord in the first list or both chords in the second list? Thank you!
       
      (ambitus-chord '(14 6) '((eb4c6 c4fs4 b4) (c4b4 b3eb5b3)))  
    • By NagyMusic
      After a short break, I'm slowly coming back to Opusmodus, mainly by working through functions examples. Looking at the gen-ambitus-series
      function example below, would someone be willing to explain why the ambitus function is not producing all diatonic pitches included in list_? I understand that I may want to use harmonic-path or similar instead, but I just wanted to ask for the learning experience. I feel like I might be missing something so I welcome the feedback!
       
      (setf range_ (gen-ambitus-series '(-10 30) (vector-smooth 0.2 (gen-white-noise 6 :seed 23)) (vector-smooth 0.2 (gen-white-noise 8 :seed 24)))) (integer-to-pitch range_) (setf list_ '((c4 d4 e4 f4 g4 a4 b4 c5) (c4 d4 e4 f4 g4 a4 b4 c5) (c4 d4 e4 f4 g4 a4 b4 c5) (c4 d4 e4 f4 g4 a4 b4 c5) (c4 d4 e4 f4 g4 a4 b4 c5) (c4 d4 e4 f4 g4 a4 b4 c5) (c4 d4 e4 f4 g4 a4 b4 c5) (c4 d4 e4 f4 g4 a4 b4 c5) )) (ambitus range_ list_) Perhaps a different example may illustrate my question better. Below we have a series of transformation of the given harmonic sequence. The pitches (pitch classes) of the harmonic sequence are retained with each individual ambitus transformation, but not when gen-ambitus-series is used (see below). Thanks!
       
      (setf omn '((h e4f5 p c5a4) (h b3d3 gs4eb2fs3) (h bb2g5cs5 gs4d4eb2) (w bb2 mp) (h g3f4cs5 p c5) (h fs5a5b3 e4b3) (h bb2) (w e2eb4) (h c5cs6 a5) (h f4g3 gs4d3) (h fs5 bb2fs5g3) (h d3e5eb4 gs4) (h a2c6 f2) (h b0 cs6c5) (h gs4d3))) (ambitus '(-6 12) omn) (setf range (gen-ambitus-series '(-30 42) (vector-smooth 0.2 (gen-white-noise 15 :seed 23)) (vector-smooth 0.2 (gen-white-noise 15 :seed 24)))) (ambitus range omn)  
       
×
×
  • Create New...