Switching from Lisp to Logo

I switched from learning lisp directly to learning a lisp dilect called logo.

I need logo for my research hence this preference. To practice algorithms I am solving some problems from the following site(here). So far I did 5 out of 99. I hope to do them all. I am using a logo compiler called liogoc. I just found out it does not have cond statement. Well, its not bad since I can use ifelse statement instead.

;P01 (*) Find the last box of a list.
;Example:
;* (my-last '(a b c d))
;(D)

to my.last :list 
 ifelse emptyp butfirst :list 
  [output first :list]
  [output my.last butfirst :list]
end 

to my.last2 :list 
 output last :list
end 


;P02 (*) Find the last but one box of a list.
;Example:
;* (my-but-last '(a b c d))
;(C D)

to my.but.last :list 
 ifelse emptyp butfirst butfirst :list 
  [output :list]
  [output my.but.last butfirst :list]
end 

;P03 (*) Find the K'th element of a list.
;The first element in the list is number 1.
;Example:
;* (element-at '(a b c d e) 3)
;C

to element.at :list :index  
 ifelse emptyp :list
  [output :list]
  [ifelse equalp :index 1 
   [output first :list]
   [output element.at butfirst :list (:index - 1)]]
end

to element.at2 :list :index 
 output item :index :list 
end 

;P04 (*) Find the number of elements of a list.

to number.of.elements :list 
 ifelse emptyp :list 
  [output 0]
  [output 1 + number.of.elements butfirst :list]
end 

to number.of.elements2 :list 
 output count :list
end

;P05 (*) Reverse a list.

to my.reverse :list 
 ifelse emptyp :list  
  [output :list]
  [output sentence (my.reverse butfirst :list) first :list]
end

to my.reverse2 :list 
        output reverse :list
end



show my.reverse [1 2 3 4 5 6 7 8 9]

Comments

Popular posts from this blog

Recursion Part 1

Back to programming competitons

Debugging Part 3