<< Chapter < Page Chapter >> Page >

Danh sách

Danh sách là một dãy có phân biệt thứ tự của các phần tử cách nhau ít nhất một khoảng trắng và đặt nằm trong cặp dấu ngoặc đơn ().

Phần tử của danh sách có thể là một nguyên tử hoặc là một danh sách.

Hằng danh sách được mở đầu bằng dấu nháy đơn ‘.

Ví dụ về các hằng danh sách:

  • ‘()Danh sách rỗng, tương đương ký hiệu NIL.
  • ‘(a 5 c)Danh sách gồm 3 phần tử.
  • ‘(3 (b c) d (e (f g)))Danh sách gồm 4 phần tử, trong đó phần tử thứ 2 và phần tử thứ 4 lại là các danh sách.

Biểu thức

Biểu thức là một nguyên tử hoặc một danh sách. Biểu thức luôn có một giá trị mà việc định trị nó theo nguyên tắc sau:

  • Nếu biểu thức là một số, thì giá trị của biểu thức là giá trị của số đó.

Ví dụ:

>25

= 25

  • Nếu biểu thức là một ký hiệu thì giá trị của biểu thức có thể là
  • Được xác định trước bởi LISP (chẳng hạn t có giá trị là T (TRUE) và nil có giá trị là NIL một danh sách rỗng) hoặc
  • Một giá trị dữ liệu của người sử dụng hoặc trong chương trình được gán cho một biến. Biến không cần phải khai báo.

Ví du:

>(setq a 3) ; Gán số 3 cho biến có tên a

= 3

>a ; hỏi giá trị của ký hiệu “a”

= 3

  • Nếu biểu thức là một danh sách có dạng (E0 E1 ... En) thì giá trị của biểu thức được xác định theo cách sau đây:
  • Phần tử đầu tiên E0 phải là một hàm đã được LISP nhận biết.
  • Các phần tử E1, E2, ..., En được định trị tuần tự từ trái sang phải. Giả sử ta có các giá trị tương ứng là V1, V2, ..., Vn
  • Hàm E0 được áp dụng cho các đối V1, V2, ..., Vn. Giá trị của hàm E0 chính là giá trị của biểu thức.

Ví dụ

>(+ 5 3 6)

= 14

>( + 4 (+ 3 5))

= 12

  • Chú ý: Nếu biểu thức dùng hàm QUOTE hoặc dấu nháy đơn sẽ không được đánh giá

Ví dụ:

>‘(+ 1 2)

= (+ 1 2)

Các hàm

Một chương trình của LISP là một hàm hoặc một hàm hợp. Các hàm có thể do LISP định nghĩa trước hoặc do lập trình viên tự định nghĩa.

Một số hàm định nghĩa trước

  • Các hàm số học: +, -, *, /, 1+, 1-, MOD, SQRT tác động lên các biểu thức số và cho kết quả là một số.

Ví dụ:

>(+ 5 6 2)

= 13

>(- 8 3)

= 5

>(- 8 3 1)

= 4

>(1+ 5) ; Tương đương (+ 5 1)

= 6

>(1- 5) ; Tương đương (- 5 1)

= 4

>(MOD 14 3)

= 2

>(sqrt 9) ; Lấy căn bậc hai của 9

= 3

  • Các hàm so sánh các số<,>,<=,>=, = và /=, cho kết quả là T hoặc NIL

Ví dụ:

>(<4 5)

= T

>(>4 (* 2 3))

= NIL

  • (EQ s1 s2) so sánh xem hai ký hiệu s1 và s2 có giống nhau hay không?

Ví dụ:

>(eq ‘tuong ‘tuong)

= T

>(eq ‘tuong ‘duong)

= NIL

>(eq ‘5 5 )

= T

  • (EQUAL o1 o2) so sánh xem đối tượng bất kỳ o1 và o2 có giống nhau hay không?

Ví dụ:

>(equal ‘(a b c) ‘(a b c))

= T

>(equal ‘(a b c) ‘( b a c))

= NIL

>(equal ‘a ‘a)

= T

  • Các hàm thao tác trên danh sách: CAR, CDR, CONS và LIST
  • (CAR L) nhận vào danh sách L, trả về phần tử đầu tiên của L.

Ví du:

>(CAR '(1 2 3))

= 1

>(CAR 3)

Error: bad argument type - 3

>(CAR nil)

= NIL

>(CAR '((a b) 1 2 3))

= (A B)

  • (CDR L) nhận vào danh sách L, trả về một danh sách bằng phần còn lại của danh sách L sau khi bỏ đi phần tử đầu tiên.

Ví dụ:

>(cdr '(1 2 3))

= (2 3)

>(cdr 3)

Error: bad argument type - 3

>(cdr nil)

= NIL

>(cdr '(1))

= NIL

>(CAR (CDR ‘(a b c)))

= B

  • Viết gộp các hàm: Ta có thể dùng hàm C..A/D..R để kết hợp nhiều CAR và CDR (có thể thay thế việc lồng nhau tới 4 cấp)

Ví du:

(CADR ‘(a b c))

= B

  • (CONS x L) nhận vào phần tử x và danh sách L, trả về một danh sách, có được bằng cách thêm phần tử x vào đầu danh sách L

Questions & Answers

physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
what is learning? discuss how any three scholars propounded on how learning is
Grace Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Ngôn ngữ lập trình. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10783/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ngôn ngữ lập trình' conversation and receive update notifications?

Ask